454 四数相加II
解题思路
这题的话,我最开始还是没有思路,我觉得这个要是暴力的话就会有好多种,就没有仔细去想了,然后我说一下我看的卡叔讲的吧,这里把他两两分组,为什么要两两分组,因为两两分好之后就可以把4个数得排列组合变成2个数的排列组合,代码如下,我只能说注意map和dict
class Solution(object):
def fourSumCount(self, nums1, nums2, nums3, nums4):
"""
:type nums1: List[int]
:type nums2: List[int]
:type nums3: List[int]
:type nums4: List[int]
:rtype: int
"""
sum1 = dict()
count = 0
for i in nums1:
for j in nums2:
if i + j in sum1.keys():
sum1[i + j] += 1
else:
sum1[i + j] = 1
for i in nums3:
for j in nums4:
res = -i - j
if res in sum1:
count += sum1[res]
return count
if __name__=="__main__":
n1=[1,3,2,5]
n2=[-1,4,6,6]
n3=[1,-3,-4,0]
n4=[0,3,2,1]
solu=Solution()
print(solu.fourSumCount(n1,n2,n3,n4))
383 赎金信
解题思路
这个和上一题很像了
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
sB=dict()
for i in magazine:
if i in sB:
sB[i]+=1
else:
sB[i]=1
for i in ransomNote:
if i in sB:
if sB[i]>0:
sB[i]-=1
else:
return False
else:
return False
return True
if __name__=="__main__":
# ransomNote = "a", magazine = "b"
# 输出:false
solu=Solution()
print(solu.canConstruct("aa","ab"))
15 三数之和
解题思路
这题其实和四数相加很像,于是我首先考虑是不是可以用hash做出来,但是我就发现了一个问题,如果我用hash的话我需要转为2数相加,这样的话我不能保证他不重复,因为我存入的是一个加和的结果,在看完卡叔的讲解后选择用双指针实现,首先先将列表排序,然后固定一个指针移动,小指针固定指针的下一个,而大指针指向最后一个,然后循环终止就是大小指针遇到的时候,效果不太好,边界要注意固定指针去重和大小指针去重
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
cur = 0
small = cur + 1
max = len(nums) - 1
nums.sort()
res = []
ans = []
sum1 = 0
while cur < len(nums):
while small < max:
sum1 = nums[cur] + nums[small] + nums[max]
if sum1 > 0:
max -= 1
if max + 1 < len(nums) - 1:
while small<max and nums[max + 1] == nums[max]:
max -= 1
elif sum1 < 0:
small += 1
if small > 1:
while small<max and nums[small - 1] == nums[small]:
small += 1
else:
ans.append(nums[cur])
ans.append(nums[small])
ans.append(nums[max])
small += 1
max -= 1
res.append(ans)
ans = []
if max + 1 < len(nums) - 1:
while small<max and nums[max + 1] == nums[max]:
max -= 1
if small > 1:
while small<max and nums[small - 1] == nums[small]:
small += 1
cur += 1
while cur < len(nums) and nums[cur - 1] == nums[cur]:
cur += 1
sum1 = 0
max = len(nums) - 1
small = cur + 1
return res
if __name__=="__main__":
l1=[-2,0,0,2,2]
solu=Solution()
print(solu.threeSum(l1))
18 四数之和
解题思路
和上题一样,但是复杂一点
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
cur1=0
cur2=cur1+1
small=cur2+1
max=len(nums)-1
res=[]
ans=[]
nums.sort()
print("【nums】",nums)
while cur1<cur2 and small<max:
sum_cur=0
while cur2<small and small<max:
sum_cur=nums[cur1]+nums[cur2]
sum_other=0
while small<max and max<len(nums) and small>0:
sum_other=sum_cur+nums[small]+nums[max]
if sum_other>target:
max-=1
if max + 1 < len(nums):
while small<max and nums[max+1]==nums[max]:
max-=1
elif sum_other<target:
small+=1
if small - 1 >= cur2:
while small<max and nums[small-1]==nums[small]:
small+=1
else:
ans.append(nums[cur1])
ans.append(nums[cur2])
ans.append(nums[small])
ans.append(nums[max])
res.append(ans)
ans=[]
small+=1
max-=1
if max+1<len(nums):
while small<max and nums[max+1]==nums[max]:
max-=1
if small-1>=cur2:
while small<max and nums[small-1]==nums[small]:
small+=1
cur2+=1
if cur2-1>=cur1:
while cur2<len(nums) and nums[cur2 - 1] == nums[cur2]:
cur2 += 1
small=cur2+1
max=len(nums)-1
cur1+=1
if cur1>0:
while cur1 < cur2 and nums[cur1 - 1] == nums[cur1]:
cur1 += 1
cur2=cur1+1
small=cur2+1
max=len(nums)-1
return res
if __name__=="__main__":
solu=Solution()
nums =[0,2,2,2,10,-3,-9,2,-10,-4,-9,-2,2,8,7]
target = 6
print(solu.fourSum(nums,target))