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

题目描述

  在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
题解:
  想复杂了,从头遍历两轮即可。
  
 1 class Solution {
 2 public:
 3     int FirstNotRepeatingChar(string str) {
 4         if (str.length() == 0)return -1;
 5         int word[128] = { 0 };
 6         for (auto a : str)
 7             word[a]++;
 8         for (int i = 0; i < str.length(); ++i)
 9             if (word[str[i]] == 1)
10                 return i;
11         return -1;
12     }
13 };
 
原文地址:https://www.cnblogs.com/zzw1024/p/11701040.html