Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Note: You may assume the string contain only lowercase letters.
給予一個字串,找到第一個不重複的字元並回傳它的索引。如果不存在則回傳 -1。
s = "leetcode" return 0. s = "loveleetcode", return 2.
class Solution { public: int firstUniqChar(string s) { unordered_map<char, int> hash; for(int i = 0; i < s.length(); i++) { hash[s[i]]++; } for(int i = 0; i < s.length(); i++) { if(hash[s[i]] == 1) return i; } return -1; } };
LeetCode
C++
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up