LeetCode C++ 387. First Unique Character in a String【String/Hash Table】簡單
Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode"
return 2.
Note: You may assume the string contains only lowercase English letters.
題意:找出字串中第一個不重複的字元。
解法 雜湊
class Solution {
public:
int firstUniqChar(string s) {
int cnt[26] = {0}, n = s.size();
for (const char &c : s) ++cnt[c - 'a'];
for (int i = 0; i < n; ++i) if (cnt[s[i] - 'a'] == 1) return i;
return -1;
}
};
提交後的執行結果:
執行用時:28 ms, 在所有 C++ 提交中擊敗了91.78% 的使用者
記憶體消耗:10.8 MB, 在所有 C++ 提交中擊敗了37.17% 的使用者
相關文章
- 演算法-First Unique Character in a String-字串中的第一個唯一字元演算法字串字元
- LeetCode C++ 204. Count Primes【Math/Hash Table】簡單LeetCodeC++
- python character stringPython
- redis string 簡單動態字串Redis字串
- [LeetCode] Rotate StringLeetCode
- [leetcode] Scramble StringLeetCode
- 簡單介紹Java String Methods(上)Java
- 簡單介紹Java String Methods(下)Java
- MySQL Invalid gbk character string 1300MySql
- Swift中String和Character的使用與總結Swift
- [CareerCup] 1.1 Unique Characters of a String 字串中不同的字元字串字元
- C++ string (淺談)C++
- C++基礎::stringC++
- Leetcode 481 Magical StringLeetCode
- Leetcode Reverse Words in a StringLeetCode
- leetcode Reverse Words in a StringLeetCode
- leetcode String to Integer (atoi)LeetCode
- Leetcode-Scramble StringLeetCode
- Leetcode - String to Integer (atoi)LeetCode
- Scramble String leetcode javaLeetCodeJava
- 簡單動態字串(simple dynamic string)SDS字串
- C++【string】用法和例子C++
- c++ primer 之stringC++
- 簡單的string類的模擬實現
- leetcode 344. Reverse StringLeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- 【Leetcode】767. Reorganize StringLeetCode
- LeetCode-Reverse Vowels of a StringLeetCode
- LeetCode-Decode StringLeetCode
- LeetCode-Reverse Words in a StringLeetCode
- Leetcode-Interleaving StringLeetCode
- String to Integer (atoi) leetcode javaLeetCodeJava
- Interleaving String leetcode javaLeetCodeJava
- Reverse Words in a String leetcode javaLeetCodeJava
- C++學習 2.5 string類C++
- C++ 額外的 string 操作C++
- C++ string型別常用操作C++型別
- String,String Builder,String Buffer-原始碼UI原始碼