?LeetCode刷題實戰(zhàn)387:字符串中的第一個唯一字符
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;
}
};
LeetCode刷題實戰(zhàn)381:O(1) 時間插入、刪除和獲取隨機元素
LeetCode刷題實戰(zhàn)382:鏈表隨機節(jié)點
LeetCode刷題實戰(zhàn)384:打亂數(shù)組
評論
圖片
表情
