美文网首页
LeetCode387. First Unique Charac

LeetCode387. First Unique Charac

作者: Yuu_CX | 来源:发表于2016-11-19 20:39 被阅读0次

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

相关文章

网友评论

      本文标题:LeetCode387. First Unique Charac

      本文链接:https://www.haomeiwen.com/subject/bmsepttx.html