class Solution {
public:
int firstUniqChar(string s) {
map<char,int> chars;
for(int i=0; i< s.size(); i++)
{
int temp = chars[s[i]];
if(temp !=0)
{
chars[s[i]]=2;
}
else
{
chars[s[i]]=1;
}
}
for(int i=0; i< s.size(); i++)
{
int temp = chars[s[i]];
if(temp ==1)
{
return i;
}
}
return -1;
}
};leetcode First Unique Character in a String
最新推荐文章于 2024-03-18 09:51:10 发布
本文介绍了一种使用C++实现的算法,该算法用于找出字符串中首次出现的唯一字符的位置。通过遍历字符串并利用map数据结构记录每个字符出现的次数来实现这一目标。
313

被折叠的 条评论
为什么被折叠?



