欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 算法训练营第五天 | 454.四数相加II\ 383. 赎金信\15. 三数之和\ 18. 四数之和

算法训练营第五天 | 454.四数相加II\ 383. 赎金信\15. 三数之和\ 18. 四数之和

2025/4/30 20:21:09 来源:https://blog.csdn.net/suwnbs/article/details/147615054  浏览:    关键词:算法训练营第五天 | 454.四数相加II\ 383. 赎金信\15. 三数之和\ 18. 四数之和

454.四数相加II

题目

在这里插入图片描述

思路与解法

第一想法:
carl的讲解: 前两个为一组,后两个为一组。遍历前两个可能的加值,再在后两组中寻找有没有前面值的负值,若有就是一组。

我认为,重点在于将问题简化为前两个数组和后两个数组的逻辑。

class Solution:def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:from collections import defaultdictsum_dict = defaultdict(int)for a in nums1:for b in nums2:sum_dict[a+b] += 1count = 0for c in nums3:for d in nums4:if (0-c-d) in sum_dict:count += sum_dict[0-c-d]return count 

383. 赎金信

题目

在这里插入图片描述

思路与解法

第一想法: 将magazine中的字母存在字典中,key是字母,value是出现的次数。然后遍历ransomNote的字母,在magazine字典中出现一次就value减一,value小于0就返回False。若字典中没有,就直接返会False。最后都通过了,就返回True。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from collections import defaultdictmagazine_dict = defaultdict(int)for a in magazine:magazine_dict[a] += 1for b in ransomNote:if b not in magazine_dict:return Falsemagazine_dict[b] -= 1if magazine_dict[b] < 0:return Falsereturn True

15. 三数之和

题目

思路与解法

第一想法: 双指针,三个数,for i in enumerate(nums),i代表第一个,low = i+1, fast = low+1,代表后面两个。然后low 和 fast遍历数组来和i相加看是不是等于0。i也不断往后遍历。
但是忽略了去重。加上去重就是对的,但是超时了。

class Solution:def threeSum(self, nums: List[int]) -> List[List[int]]:res = []nums.sort()for i, num  in enumerate(nums):low = i + 1if i > 0 and nums[i] == nums[i-1]:continuewhile low < len(nums) -1 :fast = low + 1while fast < len(nums):if nums[low] + nums[fast] + num == 0:res.append([num, nums[low], nums[fast]])while fast + 1 < len(nums) and nums[fast + 1] == nums[fast]:fast += 1fast += 1while low + 1 < len(nums) -1 and nums[low+1] == nums[low]:low += 1low += 1return res

carl的讲解: 让fast指针从最右边开始,因为排序了,所以fast(right)指向的是最大值

class Solution:def threeSum(self, nums: List[int]) -> List[List[int]]:result = []nums.sort()for i in range(len(nums)):# 如果第一个元素已经大于0,不需要进一步检查if nums[i] > 0:return result# 跳过相同的元素以避免重复if i > 0 and nums[i] == nums[i - 1]:continueleft = i + 1right = len(nums) - 1while right > left:sum_ = nums[i] + nums[left] + nums[right]if sum_ < 0:left += 1elif sum_ > 0:right -= 1else:result.append([nums[i], nums[left], nums[right]])# 跳过相同的元素以避免重复while right > left and nums[right] == nums[right - 1]:right -= 1while right > left and nums[left] == nums[left + 1]:left += 1right -= 1left += 1return result

18. 四数之和

题目

在这里插入图片描述

思路与解法

carl的讲解:

class Solution:def fourSum(self, nums: List[int], target: int) -> List[List[int]]:nums.sort()n = len(nums)result = []for i in range(n):if nums[i] > target and nums[i] > 0 and target > 0:# 剪枝(可省)breakif i > 0 and nums[i] == nums[i-1]:# 去重continuefor j in range(i+1, n):if nums[i] + nums[j] > target and target > 0: #剪枝(可省)breakif j > i+1 and nums[j] == nums[j-1]: # 去重continueleft, right = j+1, n-1while left < right:s = nums[i] + nums[j] + nums[left] + nums[right]if s == target:result.append([nums[i], nums[j], nums[left], nums[right]])while left < right and nums[left] == nums[left+1]:left += 1while left < right and nums[right] == nums[right-1]:right -= 1left += 1right -= 1elif s < target:left += 1else:right -= 1return result

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词