第一个只出现一次的字符位置

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置。若为空串,返回-1。位置索引从0开始
 
 1 class Solution {
 2 public:
 3     int FirstNotRepeatingChar(string str) {
 4         int len = str.length();
 5         if (len == 0)
 6         {
 7             return -1;
 8         }
 9         map<char,int> mm;
10         for (int i = 0;i < len ; ++i)
11         {
12             if (mm.count(str[i]) == 0)
13             {
14                 mm[str[i]] = 1;
15             }
16             else
17             {
18                 ++ mm[str[i]];
19             }
20         }
21 
22         for (int i = 0;i < len ; ++i)
23         {
24             if (mm[str[i]] == 1)
25                 return i;
26         }
27         return -1;
28     }
29 };
原文地址:https://www.cnblogs.com/xiaoyesoso/p/5159154.html