首页 > 其他 > 详细

#Leetcode# 387. First Unique Character in a String

时间:2019-01-30 20:51:30      阅读:139      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/first-unique-character-in-a-string/

 

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) {
        int len = s.length();
        unordered_map<char, int> mp;
        int ans = -1;
        
        for(int i = 0; i < len; i ++)
            mp[s[i]] ++;
        
        for(int i = 0; i < len; i ++) {
            if(mp[s[i]] == 1) {
                ans = i;
                break;
            }
        }
        return ans;
    }
};

  

FHFHFH

#Leetcode# 387. First Unique Character in a String

原文:https://www.cnblogs.com/zlrrrr/p/10339719.html

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