版权声明:本文为博主原创文章,未经博主允许不得转载。@ceezyyy11 https://blog.csdn.net/ceezyyy11/article/details/88915604
[LeetCode] 387. First Unique Character in a String (C++)
Easy
Share
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.
class Solution {
public:
int firstUniqChar(string s) {
/*
Record the number of occurrences of each character
*/
map<char,int> occurence;
for(char c:s) {
occurence[c]++;
}
for(int j=0;j<s.size();j++) {
if(occurence[s.at(j)]==1) {
return j;
}
}
return -1;
}
};
/*
Submission Detail:
Runtime: 76 ms, faster than 28.63% of C++ online submissions for First Unique Character in a String.
Memory Usage: 13 MB, less than 98.32% of C++ online submissions for First Unique Character in a String.
*/