[剑指 offer] JT34---The first character that appears only once (not before, and also after)

Sword Finger Offer Question 34

The topic is as follows

Insert picture description here

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;
    }
};

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42136832/article/details/114915270