class Solution {
public:
int FirstNotRepeatingChar(string str) {
map<char, int> count;
char res;
int len = str.length();
for(int i = 0 ; i < len ; i++){
count[str[i]]++;
}
for(int i = 0; i < len ;i++){
// count() 返回指定元素出现的次数
if(count[str[i]] == 1){
return i;
}
}
return -1;
// if(str.length()==0)
// return -1;
// for(int i=0;i<str.length()-1;i++)
// // 从pos(默认是是0,即从头开始查找)开始查找,
// // 找到第一个和str1相匹配的子串,
// // 返回该子串的起始索引位置;如果没有找到则返回string::npos
// if( str.find_first_of(str[i])==i && str.find_last_of(str[i])==i )
// return i;
// return -1;
}
};
第一个只出现一次的字符
最新推荐文章于 2024-05-29 11:38:19 发布