Re: [閒聊] 每日LeetCode

作者: JerryChungYC (JerryChung)   2024-02-05 08:52:12
https://leetcode.com/problems/first-unique-character-in-a-string
387. First Unique Character in a String
給一個字串s,找到第一個不重複的字元回傳其索引,不存在則回傳-1
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Outpue: 2
Example 3:
Input: s = "aabb"
Output: -1
思路:
用for迴圈找出只出現一次的字元
Python3 code:
作者: JIWP (JIWP)   2024-02-05 08:58:00
大師
作者: PyTorch (屁眼火炬)   2024-02-05 09:01:00
大師
作者: wu10200512 (廷廷)   2024-02-05 09:01:00
這不是哈希表ㄇ
作者: JerryChungYC (JerryChung)   2024-02-05 09:04:00
那就自己建 counter = {} :(counter = defaultdict(int)for i in s:counter[i] += 1

Links booklink

Contact Us: admin [ a t ] ucptt.com