Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"return 0.s = "loveleetcode",return 2.
Note: You may assume the string contain only lowercase letters.
思路:若正向查找和反向查找字符的下標相同,則可返回第一個只出現(xiàn)了一次的字符。
class Solution {
public:
int firstUniqChar(string s) {
for(int i=0;i<s.size();i++){
if(s.find(s[i])==s.rfind(s[i])) return i;
}
return -1;
}
};