Skip to content

[LeetCode] 387. 字符串中的第一个唯一字符 #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Animenzzzz opened this issue Sep 3, 2019 · 0 comments
Open

[LeetCode] 387. 字符串中的第一个唯一字符 #63

Animenzzzz opened this issue Sep 3, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

题目描述:

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

案例:

s = "leetcode"
返回 0.

s = "loveleetcode",
返回 2.

解题思路:遍历一遍用哈希表存好数。第二遍直接返回只出现了一次的字母的下标

C++解法:

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char,int> map;
        for(char cc:s){
            map[cc]++;
        }
        for (int i = 0; i < s.size(); i++)
        {
            if(map[s[i]] == 1) return i;
        }
        return -1;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant