Sword Finger Offer Question 34
The topic is as follows
Idea and code
Idea 1: This is related to the number of times. Everyone should think of map, save, and then search one by one.
Idea 2: But for strings, he has a find function that can be used, that is, we can use the entire character If you find it only once in the string, it is the answer. The display is very simple, just go to the code
class Solution {
public:
int FirstNotRepeatingChar(string str) {
int len=str.length();
if(!len) return -1;
int i;
for(i=0;i<str.length();i++){
if(str.find(str[i],0)==i&&str.find(str[i],i+1)==-1)
break;
}
if(i==len) return -1;
else return i;
}
};