题目

思路:
①由于字符串中只存在小写字符,则创建一个长度为26的数组保存每个字符出现的个数
②遍历字符串s,与计数数组进行比较,寻找第一个出现在字符串s中且计数为1的字符,返回其下表
否则,返回-1
class Solution {
public:
int firstUniqChar(string s)
{
int i = 0;
int *arr = new int[26]();
while(s[i]!='\0')
{
arr[s[i]-'a']++;
i++;
}
i = 0;
while(s[i]!='\0')
{
if(arr[s[i]-'a'] == 1)
return i;
i++;
}
return -1;
}
};

1349

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



