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% 的使用者
相關文章
- LeetCode C++ 204. Count Primes【Math/Hash Table】簡單LeetCodeC++
- 演算法-First Unique Character in a String-字串中的第一個唯一字元演算法字串字元
- python character stringPython
- [LeetCode] Rotate StringLeetCode
- Leetcode 481 Magical StringLeetCode
- redis string 簡單動態字串Redis字串
- Leetcode 8 String to Integer (atoi)LeetCode
- 【Leetcode】767. Reorganize StringLeetCode
- Leetcode 151 Reverse Words in a StringLeetCode
- leetcode 344. Reverse StringLeetCode
- 【Leetcode】1528. Shuffle StringLeetCode
- LeetCode C++ 316. Remove Duplicate Letters【Stack/Greedy/String】中等LeetCodeC++REM
- 簡單介紹Java String Methods(上)Java
- 簡單介紹Java String Methods(下)Java
- c++ primer 之stringC++
- C++ string (淺談)C++
- [LeetCode] 678. Valid Parenthesis StringLeetCode
- [LeetCode] 844. Backspace String CompareLeetCode
- [LeetCode] 3163. String Compression IIILeetCode
- 簡單動態字串(simple dynamic string)SDS字串
- err Invalid input of type: 'dict'. Convert to a byte, string or number first
- C++【string】用法和例子C++
- LeetCode 438. Find All Anagrams in a StringLeetCode
- LeetCode 394. Decode String All In OneLeetCode
- C++學習 2.5 string類C++
- C++ 額外的 string 操作C++
- C++ string型別常用操作C++型別
- String,String Builder,String Buffer-原始碼UI原始碼
- Failed to execute user defined function(anonfun$concatStr$1: (map<string,string>, string) => string)AIFunction
- 帶你掌握Redis資料型別:string和HashRedis資料型別
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- C++之string型別詳解C++型別
- c++中字串之string和charC++字串
- Redis 儲存物件資訊是用 Hash 還是 StringRedis物件
- redis存json資料時選擇string還是hashRedisJSON
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- [LeetCode] 2825. Make String a Subsequence Using Cyclic IncrementsLeetCodeREM