【题目】
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
【示例】
s = “leetcode”
返回 0
s = “loveleetcode”
返回 2
【代码】
【CPP】
执行用时:
12 ms, 在所有 C++ 提交中击败了99.91%的用户
内存消耗:
10.3 MB, 在所有 C++ 提交中击败了97.39%的用户
class Solution {
public:
int firstUniqChar(string s) {
int cnt[26]={0},first=0;
for(auto x:s)
cnt[x-'a']++;
for(auto x:s){
if(cnt[x-'a']==1)
return first;
first+=1;
}
return -1;
}
};