title: LEETCODE DAY7
date: 2024-02-27 10:33:15
tags:
DAY7
今日题目:LEETCODE 454.四数相加II 、383. 赎金信 、15. 三数之和 、18. 四数之和
T1
知识点:
if break、if continue和if return三者之间的区别
python语法:
table[num] = table.get(num,0) + 1,table字典中获取键为num的-值,如果num不存在于字典中,则返回默认值0。然后将获取到的值加1。
del my_dict[‘a’],del关键字被用来删除字典my_dict中的键为’a’的键值对。
本题核心在于只需判断
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
sum1=sum2=list()
n=len(nums1)
count=0
for i in range(n):
for j in range(n):
a=nums1[i]+nums2[j]
sum1.append(a)
for i in range(n):
for j in range(n):
a=nums3[i]+nums4[j]
sum2.append(a)
for i in sum1:
if -i in sum2:
count+=1
return count
错误
nums1 =
[1,2]
nums2 =
[-2,-1]
nums3 =
[-1,2]
nums4 =
[0,2]
输出
6
预期结果
2
DEBUG:与DAY6 T2同样的细节错误
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
sum1=list()
sum2=list()
n=len(nums1)
count=0
for i in range(n):
for j in range(n):
a=nums1[i]+nums2[j]
sum1.append(a)
for i in range(n):
for j in range(n):
a=nums3[i]+nums4[j]
sum2.append(a)
for i in sum1:
if -i in sum2:
count+=1
return count
又报错
nums1 =
[-1,-1]
nums2 =
[-1,1]
nums3 =
[-1,1]
nums4 =
[1,-1]
输出
4
预期结果
6
错误原因是sum2里有多个 -i,每次count应该加上sum2中含有 -i的个数
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
hashtable=dict()
sum1=list()
sum2=list()
n=len(nums1)
count=0
for i in range(n):
for j in range(n):
a=nums1[i]+nums2[j]
sum1.append(a)
for i in range(n):
for j in range(n):
a=nums3[i]+nums4[j]
sum2.append(a)
hashtable[a]=hashtable.get(a,0)+1
for i in sum1:
if -i in sum2:
count+=hashtable[-i]
return count
最后循环可改成
for i in sum1:
count+=hashtable.get(-i,0)
提交显示超时
再修改一下
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
hashtable=dict()
sum1=list()
sum2=list()
n=len(nums1)
count=0
for i in nums1:
for j in nums2:
sum1.append(i+j)
for i in nums3:
for j in nums4:
a=i+j
sum2.append(a)
hashtable[a]=hashtable.get(a,0)+1
for i in sum1:
count+=hashtable.get(-i,0)
return count
最终AC 时间复杂度O( n 2 n^2 n2)
T2
类似DAY6 T1 242.有效的字母异位词
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
record=[0]*26
for i in ransomNote:
record[ord(i)-ord('a')]-=1
for i in magazine:
record[ord(i)-ord('a')]+=1
for i in record:
if i <0:
return False
return True
也可以用字典
题解中最快速的解法
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
r_set = set(ransomNote)
for char in r_set:
if char not in magazine or ransomNote.count(char) > magazine.count(char):
return False
return True
T3
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
slow=0
fast=0
res=list()
while fast < len(nums):
slow=0
below=list()
while slow <fast:
if -nums[fast]-nums[slow] in below:
res.append([nums[fast],nums[slow],-nums[fast]-nums[slow]])
below.append(nums[slow])
slow+=1
fast+=1
return res
错误
输入
nums =
[-1,0,1,2,-1,-4]
输出
[[1,0,-1],[-1,1,0],[-1,2,-1]]
预期结果
[[-1,-1,2],[-1,0,1]]
先排序再删除重复情况
python语法:
nums=sorted(nums)
或者
nums.sort()
用双指针法写
细节很多每一个判断都要仔细考虑否则会报错
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
slow=0
fast=len(nums)-1
res=list()
for i in range(len(nums)):
if i >0 and nums[i-1]==nums[i]:
continue
slow=i+1
fast=len(nums)-1
while slow <fast:
if nums[i]+nums[slow]+nums[fast]>0:
fast-=1
elif nums[i]+nums[slow]+nums[fast]>0:
slow+=1
else:
res.append([nums[i],nums[slow],nums[fast]])
while slow<fast and nums[fast-1]==nums[fast]:
fast-=1
while slow<fast and nums[slow]==nums[slow+1]:
slow+=1
fast-=1
return res
错误
nums =
[-1,0,1,2,-1,-4]
输出
[[-4,-1,2],[-4,0,1],[-1,-1,2],[-1,0,1]]
预期结果
[[-1,-1,2],[-1,0,1]]
while里elif 处不等号打反了
while slow <fast:
if nums[i]+nums[slow]+nums[fast]>0:
fast-=1
elif nums[i]+nums[slow]+nums[fast]<0:
slow+=1
修改后AC
T4
思路与T3类似。需要注意的是此题之所以比T1复杂是因为还要去除重复的解
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
res=list()
for i in range(len(nums)):
if i >0 and nums[i-1]==nums[i]:
continue
for j in range(i+1,len(nums)):
if j >i+1 and nums[j-1]==nums[j]:
continue
slow=j+1
fast=len(nums)-1
while slow < fast:
if nums[i]+nums[j]+nums[slow]+nums[fast]>target:
fast-=1
elif nums[i]+nums[j]+nums[slow]+nums[fast]<target:
slow+=1
else:
res.append([nums[i],nums[j],nums[slow],nums[fast]])
while slow<fast and nums[fast]==nums[fast-1]:
fast-=1
while slow<fast and nums[slow]==nums[slow+1]:
slow+=1
fast-=1
return res
重点在于去重步骤