【Leetcode387】First Unique Character in a String

class Solution {
public:
    int firstUniqChar(string s) {
        vector<int> v(26, -1);
        for(size_t index = 0; index < s.length(); ++index){
            if(v[s[index]-'a'] == -1){
                v[s[index]-'a'] = index;
            }else if(v[s[index]-'a'] >= 0){
                v[s[index]-'a'] = -2;
            }
        }
        int result = s.length();
        for(const auto& num : v){
            if(num >= 0 && num < result)
                result = num;
        }
        if(result == s.length())
            return -1;
        else
            return result;
    }
};
发布了112 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_39458342/article/details/104883309
今日推荐