标签:位置 ++ size 链接 字符串长度 nbsp for mic not
题目链接:第一个只出现一次的字符
题意:在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
题解:map统计。再查找。STL大法好
代码:
1 class Solution { 2 public: 3 int FirstNotRepeatingChar(string str) { 4 int len = str.size(); 5 if(len == 0) return -1; 6 map<char,int> mp; 7 for(int i = 0; i < len ;i++){ 8 mp[str[i]]++; 9 } 10 for(int i = 0; i < len ;i++){ 11 if(mp[str[i]] == 1) return i; 12 } 13 return 0; 14 } 15 };
标签:位置 ++ size 链接 字符串长度 nbsp for mic not
原文地址:https://www.cnblogs.com/Asumi/p/12405671.html