1,题目
给定一个字符串 s
,找到 它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1
。
2,代码
class Solution {
public:int firstUniqChar(string s) {//记数排序int coutArr[26] = {0};//统计字符出现的次数for (auto ch : s){coutArr[ch-'a']++;}//返回第一个出现一次的字符for(size_t i = 0; i < s.size(); ++i){if(coutArr[s[i] - 'a'] == 1)return i;}return -1;}
};