剑指offer(三)33题 第一次只出现一次的字符

题目描述

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

思路 :哈希表啊  map啊  

直接上代码:

class Solution {
public:
    int FirstNotRepeatingChar(string str)
    {
        int n=str.size();
        if(n==0)
            return -1;
        unordered_map<int,int>map;
        for(auto c:str)
            map[c]++;
        for(int i=0;i<n;i++)
        {
            if(map[str[i]]==1)
                return i;
        }
        return -1;
    }
};

猜你喜欢

转载自blog.csdn.net/langxue4516/article/details/81700525