题解 | #第一个只出现一次的字符#
第一个只出现一次的字符
http://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c
可以使用暴力求解:
class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        for(int i =0; i < str.length(); ++i){
            bool hasSeem = false;
            for(int j = 0; j < str.length(); ++j){
                if(i !=j && str[i] == str[j]){
                    hasSeem = true;
                    break;
                }
            }
            if(!hasSeem) return i;
        }
        return -1;
    }
}; 使用 map 也行:
class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        map<char, int> data;
        for(int i =0; i < str.length(); ++i){
            ++data[str[i]];
        }
        for(int i = 0; i < str.length(); ++i){
            if(data[str[i]] == 1) return i;
        }
        return -1;
    }
}; 或者是使用 hash
class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        unordered_map<char, int> data;
        for(int i =0; i < str.length(); ++i){
            ++data[str[i]];
        }
        for(int i = 0; i < str.length(); ++i){
            if(data[str[i]] == 1) return i;
        }
        return -1;
    }
}; 
 查看20道真题和解析
查看20道真题和解析
 投递影石Insta360等公司10个岗位
投递影石Insta360等公司10个岗位