第一次只出现一次的字符

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        if (str.empty())
            return -1;
        const int table=256;
        unsigned int hash[table];
        for (int i=0;i<table;i++)
            hash[i]=0;
        int i=0;
        while(str[i]!='\0')//for (unsigned int i=0;i<str.size();i++)
        {
            hash[str[i++]]++;
        }
        for (int j=0;j<str.size();j++)
        {
            if (hash[str[j]]==1)
                return j;
        }
        return -1;
    }
};

猜你喜欢

转载自blog.csdn.net/lidanyang666/article/details/80096932