Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
s = "leetcode"
返回 0
s = "loveleetcode"
返回 2
class Solution {
public:
int firstUniqChar(string s) {
map<char, int> m;
for(int i = 0; i < s.length(); i ++){
m[s[i]] ++;
}
for(int i = 0; i < s.length(); i ++) {
if(m[s[i]] == 1) return i;
}
return -1;
}
};
LeetCode1-380題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)381:O(1) 時間插入、刪除和獲取隨機(jī)元素
LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)
LeetCode刷題實(shí)戰(zhàn)383:贖金信
LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組
LeetCode刷題實(shí)戰(zhàn)385:迷你語法分析器
聯(lián)系客服