首页 > 其他 > 详细

LeetCode 387. First Unique Character in a String

时间:2018-12-03 22:49:02      阅读:201      评论:0      收藏: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) {
        map<char, int> checked;
        for(int i=0; i<s.size(); i++)
            checked[s[i]]++;
        int index=0;
        for(; index<s.size(); index++)
            if(checked[s[index]]==1)
               break;
        return index==s.size()?-1:index;
    }
};

LeetCode 387. First Unique Character in a String

原文:https://www.cnblogs.com/A-Little-Nut/p/10061299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!