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

#Leetcode# 387. First Unique Character in a String

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

标签:sum   break   mic   let   class   family   c99   ase   string   

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

标签:sum   break   mic   let   class   family   c99   ase   string   

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

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