387. First Unique Character in a String
和剑指offer上有个题很像,但当时没考虑到比如只有cc,整个字符完全都是重复的情况
class Solution { public: int firstUniqChar(string s) { int length = s.size(); if(length <= 0) return -1; vector<int> result(256); for(int i = 0;i < 256;i++) result[i] = -1; for(int i = 0;i < length;i++){ if(result[s[i]] == -1) result[s[i]] = i; else result[s[i]] = -2; } int min_num = 0x7FFFFFFF; for(int i = 0;i < 256;i++){ if(result[i] < min_num && result[i] >= 0) min_num = result[i]; } if(min_num == 0x7FFFFFFF) min_num = -1; return min_num; } };