# 查找,如果强调存在,用SET,如果强调个数或有时需要顺序,用DIC
#202 快乐数
class Solution:
def isHappy(self, n: int) -> bool:
s = set()
while 1:
print(s)
tmp = 0
while n != 0:
tmp += (n%10) * (n%10)
n = n // 10
if tmp == 1:
return True
else:
if tmp in s:
return False
else:
s.add(tmp)
n = tmp
#205
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
sdic = {}
tdic = {}
for i in range(len(s)):
if s[i] not in sdic and t[i] not in tdic:
sdic[s[i]] = i
tdic[t[i]] = i
else:
if s[i] not in sdic or t[i] not in tdic:
return False
else:
if (sdic.get(s[i]) != tdic.get(t[i])):
return False
return True
#1 两数求和
from collections import defaultdict
class Solution:
def twoSum(self, nums, target):
dic = defaultdict(int)
for i, v in enumerate(nums):
if (target-v) in dic:
return [dic[target-v], i]
else:
dic[v] = i
return -1
#15 3Sum *******
import numpy
class Solution:
def threeSum(self, nums):
lnums = sorted(list(nums))
res = []
for i,v in enumerate(lnums):
if v>0:
return res
if i>0 and lnums[i]==lnums[i-1]:
continue
start = i+1
end = len(lnums)-1
target = 0 - lnums[i]
while start < end:
if lnums[start] + lnums[end] == target:
res.append([lnums[i], lnums[start], lnums[end]])
while(start < end and lnums[start]==lnums[start+1]):
start=start+1
while(start < end and lnums[end]==lnums[end-1]):
end=end-1
start += 1
end -= 1
elif lnums[start] + lnums[end] > target:
end -= 1
else:
start += 1
return res
#18 四数之和
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
#16 最接近的三个数之和
class Solution:
def threeSumClosest(self, nums, target):
lnums = sorted(list(nums))
length = len(lnums)
res = lnums[0] + lnums[1] + lnums[2]
for i,v in enumerate(lnums):
if i>0 and lnums[i]==lnums[i-1]:
continue
start = i+1
end = length-1
while start < end:
tmp_sum = lnums[i] + lnums[start] + lnums[end]
if tmp_sum == target:
return tmp_sum
if abs(tmp_sum - target) < abs(res-target):
res = tmp_sum
if tmp_sum-target < 0:
start += 1
else:
end -= 1
return res
so = Solution()
print(so.threeSumClosest([-1,2,1,-4], 1))
#454 4sum two
from collections import Counter
class Solution:
def fourSumCount(self, A, B, C,D):
a = Counter(A)
b = Counter(B)
c = Counter(C)
d = Counter(D)
ab = {}
cd = {}
result = 0
for i in a:
for j in b:
ab[i+j] = ab.get(i+j, 0) + a[i] * b[j]
for i in c:
for j in d:
if 0-(i+j) in ab:
result += c[i] * d[j] * ab[-i-j]
return result
so = Solution()
print(so.fourSumCount([ 1, 2], [-2,-1], [-1, 2], [0, 2]))
#49 字母异位词分组
from collections import Counter
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs):
dic = defaultdict(list)
for i in strs:
tmp = sorted(i)
dic[tuple(tmp)].append(i)
return list(dic.values())
so = Solution()
print(so.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))
# 447 回旋镖的数量 查找表很简单
class Solution:
def numberOfBoomerangs(self, points):
# 149 直线上最多的点数(重做)
from collections import defaultdict
from collections import Counter
class Solution:
def kb(self, x, y):
x = list(map(float,x))
y = list(map(float,y))
if x[0] == y[0]:
return ['k', x[0]]
k = (x[1]-y[1]) * 1000 / (x[0]-y[0]) * 1000
b = x[1]-(k*x[0])
return [k,b]
def maxPoints(self, points):
if len(points)<=2:
return len(points)
points_numbers = Counter(tuple(point) for point in points)
dic = defaultdict(list)
points = list(points_numbers.keys())
print(points_numbers)
if len(points) <= 1:
return points[0]
for i in range(len(points)):
i_numbers = points_numbers[points[i]]
for j in range(i+1, len(points)):
j_numbers = points_numbers[points[j]]
kb = tuple(self.kb(points[i],points[j]))
print(kb, points[i] ,points[j])
print(dic)
if points[i] not in dic[kb]:
for k in range(i_numbers):
dic[kb].append(points[i])
if points[j] not in dic[kb]:
for k in range(j_numbers):
dic[kb].append(points[j])
print(dic)
result = 0
for i in dic:
tmp = len(dic[i])
if tmp > result:
result = tmp
return result
so = Solution()
print(so.maxPoints([[0,0],[94911151,94911150],[94911152,94911151]]))
#217 存在重复元素
#直接用Counter就行
from collections import defaultdict
#219 存在重复元素 II
class Solution:
def containsNearbyDuplicate(self, nums, k):
dic={}
for i in range(len(nums)):
if nums[i] in dic and i-dic[nums[i]]<=k:return True
dic[nums[i]]=i
return False
so = Solution()
print(so.containsNearbyDuplicate([1,2,3,1], 3))
#217 比219更简单