本文主要是介绍387. First Unique Character in a String(Leetcode每日一题-2020.12.23),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Problem
Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist, return -1.
Note: You may assume the string contains only lowercase English letters.
Example
s = “leetcode”
return 0.
s = “loveleetcode”
return 2.
Solution
class Solution {
public:int firstUniqChar(string s) {unordered_map<char, int> hash;for (auto c: s) hash[c] ++ ;for (int i = 0; i < s.size(); i ++ )if (hash[s[i]] == 1)return i;return -1;}
};
这篇关于387. First Unique Character in a String(Leetcode每日一题-2020.12.23)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!