第一个只出现一次的字符+数组中重复的数字

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
思路1:利用map保存每个字母的值和出现的次数,判断第一个只出现一次的字符

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        if(str.empty())
            return -1;
        map<char, int> mp; //定义一个map
        for(int i = 0; i < str.size(); i++)
            mp[str[i]]++;  //将所有字符和出现的次数保存到map中
        for(int i = 0; i < str.size(); i++){
            if(mp[str[i]] == 1) //判断第一个出现一次的字符
                return i;
        }
        return -1;  //如果没有找到则返回-1
    }
};

思路2:利用hash表

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        if(str.empty())
            return -1;
        unsigned int hashtime[256] = {0};
        for(int i = 0; i < str.size(); i++)
            hashtime[str[i]]++;  //在hash表中统计各字母出现次数
        for(int i = 0; i < str.size(); i++){
            if(hashtime[str[i]] == 1) //直接访问hash表获得次数
                return i;
        }
        return -1;
    }
};

题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
思路1:同上一个题目,利用map保存每个字母的值和出现的次数,判断出现大于一次的字符

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        if(numbers == NULL)
            return false;
        for(int i = 0; i < length; i++){
            if(numbers[i] < 0 || numbers[i] > length - 1)//判断每个数是否合法,在(0~n-1)之间
                return false;
        }
        map<int, int> mp;
        for(int i = 0; i < length; i++)
            mp[numbers[i]]++;  //统计次数
        for(int i = 0; i < length; i++){
            if(mp[numbers[i]]>1){
                duplication[0] = numbers[i];
                return true;
            }
        }
        return false;
    }
};

思路2:利用map,计算每个数出现的次数,只要有次数不为1的,则输出并返回true

最简单的方法:构造一个容量为N的辅助数组B,原数组A中每个数对应B中下标,首次命中,B中对应元素+1。如果某次命中时,B中对应的不为0,说明,前边已经有一样数字了,那它就是重复的了。
举例:A{1,2,3,3,4,5},刚开始B是{0,0,0,0,0,0},开始扫描A。
A[0] = 1  {0,1,0,0,0,0}
A[1] = 2 {0,1,1,0,0,0}
A[2] = 3 {0,1,1,1,0,0}
A[3] = 3 {0,1,1,2,0,0},到这一步,就已经找到了重复数字。
A[4] = 4 {0,1,1,2,1,0}
A[5] = 5 {0,1,1,2,1,1}

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        if(numbers==NULL)
            return false;
        for(int i = 0; i < length; i++){
            if(numbers[i] < 0 || numbers[i] > length - 1)
                return false;
        }
        map<int,int> mp;
        for(int i = 0; i < length; i++){
            if(mp[numbers[i]]==0) //如果数值还没有出现过,则次数+1
                mp[numbers[i]]++;
            else{  //如果数值已经出现过,次数不为0
                duplication[0] = numbers[i]; //输出次数不为0的数
                return true; //返回true
            }
        }
        return false;
    }
};

点赞