242. 有效的字母異位詞
- 思路
- example
- hash: 數(shù)組, size: 26
- ord('a)
- 遍歷完t后, 再檢查一遍table_s,全部為0說明匹配。
- 復(fù)雜度. 時間:O(m+n), 空間: O(1) = O(26)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
table_s = [0] * 26
for ch in s:
table_s[ord(ch)-ord('a')] += 1
for ch in t:
table_s[ord(ch)-ord('a')] -= 1
for i in range(26):
if table_s[i] != 0:
return False
return True
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
table = collections.defaultdict(int)
for ch in s:
table[ch] += 1
for ch in t:
table[ch] -= 1
if table[ch] < 0:
return False
for key, val in table.items():
if table[key] != 0:
return False
return True
- Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
- 對于進(jìn)階問題,Unicode 是為了解決傳統(tǒng)字符編碼的局限性而產(chǎn)生的方案,它為每個語言中的字符規(guī)定了一個唯一的二進(jìn)制編碼。而 Unicode 中可能存在一個字符對應(yīng)多個字節(jié)的問題,為了讓計算機(jī)知道多少字節(jié)表示一個字符,面向傳輸?shù)木幋a方式的 UTF?8 和 UTF?16 也隨之誕生逐漸廣泛使用,具體相關(guān)的知識讀者可以繼續(xù)查閱相關(guān)資料拓展視野,這里不再展開。
回到本題,進(jìn)階問題的核心點(diǎn)在于「字符是離散未知的」,因此我們用哈希表維護(hù)對應(yīng)字符的頻次即可。同時讀者需要注意 Unicode 一個字符可能對應(yīng)多個字節(jié)的問題,不同語言對于字符串讀取處理的方式是不同的。 - hash: 字典
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
table_s = collections.defaultdict(int)
table_t = collections.defaultdict(int)
for ch in s:
table_s[ch] += 1
for ch in t:
table_t[ch] += 1
return table_s == table_t
349. 兩個數(shù)組的交集
- 思路
- example
- hash: set (兩個)
- set1
- result set
- 復(fù)雜度. 時間:O(m+n), 空間: O(m+n)
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set1 = set()
for num in nums1:
set1.add(num)
res = set()
for num in nums2:
if num in set1 and num not in res:
res.add(num)
return list(res)
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set1 = set(nums1)
set2 = set(nums2)
res = set()
for num in nums1:
if num in set1 and num in set2:
res.add(num)
return list(res)
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set1 = set()
for num in nums1:
set1.add(num)
set2 = set()
for num in nums2:
if num in set1:
set2.add(num)
return list(set2)
202. 快樂數(shù)
- 思路
- example
- hash: set
用set來處理cycle
- 復(fù)雜度.
class Solution:
def isHappy(self, n: int) -> bool:
def compute_sum(num):
sum_ = 0
while num:
rem = num % 10
sum_ += rem ** 2
num //= 10
return sum_
used = set()
while True:
n = compute_sum(n)
if n == 1:
return True
if n in used:
return False
used.add(n)
class Solution:
def isHappy(self, n: int) -> bool:
def sumOfSquare(n):
res = 0
while n:
res += (n % 10) ** 2
n //= 10
return res
if n == 1:
return True
path = set()
path.add(n)
while n != 1:
n = sumOfSquare(n)
if n in path:
return False
else:
path.add(n)
return True
1. 兩數(shù)之和
- 思路
- example
- hash: dict
- record[nums[i]] = i
- 復(fù)雜度. 時間:O(n), 空間: O(n)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
hash_map = collections.defaultdict(int)
for i in range(n):
if target - nums[i] in hash_map:
return [hash_map[target-nums[i]], i]
hash_map[nums[i]] = i
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
record = collections.defaultdict(int)
for i in range(n):
if nums[i] in record:
return [record[nums[i]], i]
record[target-nums[i]] = i