LeetCode刷题实战387:字符串中的第一个唯一字符
程序IT圈
共 1516字,需浏览 4分钟
·
2021-09-22 20:04
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刷题实战381:O(1) 时间插入、删除和获取随机元素
评论