LeetCode387. First Unique Charac

2016-11-19  本文已影响0人  Yuu_CX

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"return 0.s = "loveleetcode",return 2.
Note: You may assume the string contain only lowercase letters.
思路:若正向查找和反向查找字符的下标相同,则可返回第一个只出现了一次的字符。

class Solution {
public:
    int firstUniqChar(string s) {
        for(int i=0;i<s.size();i++){
            if(s.find(s[i])==s.rfind(s[i])) return i;
        }
        return -1;
    }
};
上一篇下一篇

猜你喜欢

热点阅读