242.有效的字母异位词
思路:26位的array,每个分别对应a, b, c..., z,如果遇到一个字母就++,如果两个array一样则为anagram
hint: to initiate an array with n elements carrying value 0:
arr=[]
arr = [0 for i in range(n)]
print(arr)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
anagram_table = [0 for i in range(26)]
for i in range(len(s)):
anagram_table[ord(s[i]) - ord('a')] += 1
for i in range(len(t)):
anagram_table[ord(t[i]) - ord('a')] -= 1
print(anagram_table)
if anagram_table == [0 for i in range(26)]:
return True
else:
return False
349. 两个数组的交集
学会set的用法,
a = set()
b = {'a', 'b'}
a.add('c')
b.remove('a')
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
table = set()
result = set()
for i in range(len(nums1)):
table.add(nums1[i])
for i in range(len(nums2)):
if nums2[i] in table:
result.add(nums2[i])
return result
202. 快乐数
1. 两数之和
标签:202,随想录,len,range,set,anagram,table,ord,两数 From: https://www.cnblogs.com/miramira/p/18094466