剑指offer——52第一个只出现一次的字符

时间:2023-03-09 17:14:05
剑指offer——52第一个只出现一次的字符

题目描述

  在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
题解:
  想复杂了,从头遍历两轮即可。
  
 class Solution {
public:
int FirstNotRepeatingChar(string str) {
if (str.length() == )return -;
int word[] = { };
for (auto a : str)
word[a]++;
for (int i = ; i < str.length(); ++i)
if (word[str[i]] == )
return i;
return -;
}
};