class Solution {
public:
int firstUniqChar(string s) {
int x[26]={0};
for(int i = s.length()-1; i >= 0; i--){
if(x[s[i]-'a'] == 0) x[s[i]-'a'] = i+1;
else if(x[s[i]-'a'] > 0) x[s[i]-'a'] = -1;
}
int ans = -1;
for(int i = 0;i < 26;i++){
if(x[i] == -1 || x[i] == 0) continue;
if(ans == -1)
ans = x[i]-1;
ans = min(ans,x[i]-1);
}
return ans;
}
};
leetcode 387. First Unique Character in a String
最新推荐文章于 2024-11-01 15:19:21 发布