码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 387. First Unique Character in a String

时间:2018-12-03 23:01:53      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:examples   note   tco   let   cte   ==   and   i++   cas   

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

标签:examples   note   tco   let   cte   ==   and   i++   cas   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!