第454题.四数相加II
算法思想
因为是四个数组,因此不需要考虑类似单个数组内的重复情况,只需要将前两个数组用笛卡尔积排列出所有可能性,再用hashmap记录前两个数组的所有的和的次数。再排列出后两个数组所有和的可能性,如果和hashmap的键值和为零,则计数+1
注意点和易错点
最后count += haspmap[key],而不是+=1
TODO
代码
count = 0
hashmap = dict()
for num1 in nums1:
for num2 in nums2:
if num1 + num2 not in hashmap:
hashmap[num1 + num2] = 1
else:
hashmap[num1 + num2] += 1
for num3 in nums3:
for num4 in nums4:
target = - num3 - num4
if target in hashmap:
count += hashmap[target]
return count
383. 赎金信
算法思想
判断randomNote中的字符是不是全部包含在magzine中,因为是赎金信怕被认出字迹来,所以magzine中每个字母都只能用一次。
注意点和易错点
这道题和242有效的字母异位词有点像,但是字母异位词要求两个词的字母构成完全一致,因此在判断字母频率时是严格==0,所以有!=0的情况出现就判错,而这里是要求magzine的字母次数>赎金信的字母次数,所以>=就行。
TODO
代码
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
arr = [0] * 26
for item in magazine:
arr[ord(item) - ord('a')] += 1
for item in ransomNote:
if arr[ord(item) - ord('a')] == 0:
return False
else:
arr[ord(item) - ord('a')] -= 1
return True
15. 三数之和
算法思想
- 因为从一个数组中要找到三个不同的数字且和为零,因此首先需要进行排序。
- 那为什么要用到双指针呢,和map又有什么思路联系,但是又为什么不用map呢?
注意点和易错点
third 重置的位置是在second循环外
还可以增加nums[0] > 0就跳出循环来剪枝
TODO
代码
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
nums.sort()
ans = list()
for first in range(n):
if first > 0 and nums[first] == nums[first - 1]:
continue
target = -nums[first]
third = n - 1
for second in range(first + 1, n):
# third = n - 1
if second > first + 1 and nums[second] == nums[second - 1]:
continue
while second < third and nums[second] + nums[third] > target:
third -= 1
if second == third:
break
if nums[second] + nums[third] == target:
ans.append([nums[first], nums[second], nums[third]])
return ans
第18题. 四数之和
算法思想
三数之和是用一层for循环来确认nums[i]为target,四数之和就是用两层for循环来确认nums[i]+nums[j]为target。Q:那两层for循环就是直接笛卡尔积就好了吗?
注意点和易错点
- 每一种循环枚举到的下标必须大于上一重循环枚举到的下标;
- 同一重循环中,如果当前元素与上一个元素相同,则跳过当前元素。
- while left < right 少了这一句答案会出问题
TODO
代码
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n = len(nums)
nums.sort()
ans = list()
for i in range(n-3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i + 1, n - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
new_tar = target - nums[i] - nums[j]
right = n - 1
for left in range(j + 1, n - 1):
if left > j + 1 and nums[left] == nums[left - 1]:
continue
while left < right and nums[left] + nums[right] > new_tar:
right -= 1
if left == right:
break
if nums[left] + nums[right] == new_tar:
ans.append([nums[i], nums[j], nums[left], nums[right]])
return ans