代码随想录算法训练营第七天 | 454.四数相加II, 383. 赎金信, 15. 三数之和 & 18. 四数之和
454. 4Sum II
LeetCode Link: 4Sum II
题目链接/文章讲解/视频讲解: 四数相加II
我的代码:
C++:
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
std::unordered_map<int, int> map;
int result = 0;
for (int a : nums1) {
for (int b : nums2) {
map[a+b]++;
}
}
for (int c : nums3) {
for (int d : nums4) {
int target = 0 - (c + d);
if (map.find(target) != map.end()) {
result += map[target];
}
}
}
return result;
}
};
Python:
from collections import defaultdict
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
# Time Complexity: O(n^2)
# Space Compllexity: O(n)
record = defaultdict(int)
result = 0
for a in nums1:
for b in nums2:
record[a+b] += 1
for c in nums3:
for d in nums4:
target = 0 - (c+d)
result += record[target]
return result
383. Ransom Note
题目链接:ransom note
题目链接/文章讲解: 代码随想录ransom note
我的代码:
C++:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
// Time Complexity: O(n)
// Space Complexity: O(1)
int record[26] = {0};
if (magazine.size() < ransomNote.size()) {
return false;
}
for (int i=0; i < ransomNote.size(); i++) {
record[ransomNote[i] - 'a']++;
}
for (int i=0; i < magazine.size(); i++) {
record[magazine[i] - 'a']--;
}
for (int i=0; i < 26; i++) {
if (record[i] > 0) {
return false;
}
}
return true;
}
};
Python:
from collections import defaultdict
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
# Time Complexity: O(n)
# Space Complexity: O(1)
record = [0] * 26
for c in ransomNote:
record[ord(c) - ord('a')] += 1
for c in magazine:
if record[ord(c) - ord('a')] > 0:
record[ord(c) - ord('a')] -= 1
for val in record:
if val > 0:
return False
return True
15. 3 Sum
题目链接:3 sum
题目链接/文章讲解/视频讲解: 三数之和
我的代码:
C++:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
// Time Complexity: O(n^2)
// Space Complexity: O(log(n)) The typical implementation of std::sort in C++ is an introsort, which uses O(log(n))
std::vector<std::vector<int>> result;
sort(nums.begin(), nums.end());
for (int i=0; i<nums.size(); i++) {
if (nums[i] > 0) {
break;
}
if (i > 0 && nums[i] == nums[i-1]) {
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (left < right) {
int total = nums[i] + nums[left] + nums[right];
if (total > 0) {
right--;
} else if (total < 0) {
left++;
} else {
result.push_back(std::vector<int>{nums[i], nums[left], nums[right]});
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
right--;
}
}
}
return result;
}
};
Python:
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# Time Complexity: O(n^2)
# Space Complexity: O(n) sort in Python needs O(n) auxiliary space in the worst case.
nums.sort()
result = []
for i in range(len(nums)):
if nums[0] > 0:
return
if i > 0 and nums[i] == nums[i-1]:
continue
left = i+1
right = len(nums)-1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total < 0:
left += 1
elif total > 0:
right -=1
else:
result.append([nums[i], nums[left], nums[right]])
while left < right and nums[left] == nums[left+1]:
left += 1
while left < right and nums[right] == nums[right-1]:
right -= 1
left += 1
right -= 1
return result
18. 4 Sum
题目链接:4 sum
题目链接/文章讲解/视频讲解: 四数之和
我的代码:
这题太累了 只写了python 下次再刷争取加上C++
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
result = []
nums.sort()
print(nums)
for i in range(len(nums)):
if nums[i] > 0 and target > 0 and nums[i] > target:
return result
if i > 0 and nums[i] == nums[i-1]:
continue
for j in range(i + 1, len(nums)):
current = nums[i] + nums[j]
if target > 0 and current > target:
break
if j > i + 1 and nums[j] == nums[j-1]:
continue
left, right = j + 1, len(nums) - 1
while left < right:
total = nums[i] + nums[j] + nums[left] + nums[right]
if total > target:
right -= 1
elif total < target:
left += 1
else:
result.append([nums[i], nums[j], nums[left], nums[right]])
while left < right and nums[right] == nums[right - 1]:
right -= 1
while left < right and nums[left] == nums[left + 1]:
left += 1
right -= 1
left += 1
return result
总结
又是一个快速的总结
每日一感叹卡尔学长太厉害了 简直男神 怎么能做到做题这么细致 讲题这么清晰 什么时候能做到像他一样
但也发现自己这一轮刷题状态变了 之前怎么也跟不上 尝试过各种刷leetcode的办法,这次就是每天就算凌晨了 还是坚持刷一天的量 并且有点享受啊 主要是因为知道总有靠谱的讲解视频在背后兜着吧
总之就是继续加油!
下次刷可以补充的:
理解后两题的哈希算法,这次没有读透,只会双指针
补充C++版 4 sum
补充解题思路,目前直接用的代码随想录链接,有时间希望可以自己也写一个