Leetcode454.四数相加II
题目链接:454. 四数相加 II
C++:
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int, int> map;
for(int a: nums1)
{
for(int b: nums2)
{
map[a+b]++;
}
}
int count = 0;
for(int c: nums3)
{
for(int d: nums4)
{
int target = -(c + d);
if(map.find(target) != map.end())
count += map[target];
}
}
return count;
}
};
Python:
lambda函数:
(1)用法:lambda 参数列表 : 表达式
(2)lambda函数是匿名的,但是有自己的输入输出和名称空间
(3)举例:lambda x, y : x + y :输入为x值和y值,输出为它们的和x+y的值
(4)和defaultdict结合使用:r = defaultdict(lambda : 0),输出的字典r的所有value值都为0
python数据类型——字典dict:
(1)定义:字典元素以键值对存在:key(键):value(值),d = {}
(2)d[key] = d.get(key,0)+1:d[key]表示键为key的值value,找不到则报错;d.get(key,0)表示找到键为key的值,找不到则赋值为0
from collections import defaultdict
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
map = defaultdict(lambda: 0)
count = 0
for a in nums1:
for b in nums2:
map[a+b] += 1
for c in nums3:
for d in nums4:
count += map.get(-(c+d), 0)
return count
Leetcode383. 赎金信
题目链接:383. 赎金信
C++:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> hash(26, 0);
for(auto i: ransomNote)
{
hash[i - 'a']++;
}
for(auto j: magazine)
{
hash[j - 'a']--;
}
for(int a : hash)
{
if(a > 0)
return false;
}
return true;
}
};
Python:
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
hashmap = {}
for i in magazine:
hashmap[i] = hashmap.get(i, 0) + 1
for j in ransomNote:
if hashmap.get(j, 0) == 0:
return False
hashmap[j] -= 1
return True
Leetcode15. 三数之和
题目链接:15. 三数之和
C++:
使用sort对数组进行排序需要包含头文件#include<algorithm>:
(1)对数组升序排序:sort(nums.begin(), nums.end());
(2)对数组降序排序:sort(nums.begin(), nums.end(), greater<int>());
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> result;
for(int i=0; i < nums.size(); i++)
{
if(nums[i] > 0)
return result;
//去重i
if(i > 0 && nums[i] == nums[i-1])
continue;
int left = i+1;
int right = nums.size()-1;
while(left < right)
{
if(nums[i] + nums[left] + nums[right] < 0)
{
left++;
}
else if(nums[i] + nums[left] + nums[right] > 0)
{
right--;
}
else
{
result.push_back(vector<int>{nums[i], nums[left], nums[right]});
while(left < right && nums[left] == nums[left+1])
left++;
while(left < right && nums[right] == nums[right-1])
right--;
left++;
right--;
}
}
}
return result;
}
};
Python:
排序:
(1)python升序排序,列表本身修改:
nums.sort()
(2)python升序排序,返回新列表:
sorted(nums)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
result = []
nums.sort()
for i in range(len(nums)):
if nums[i] > 0:
return result
if i > 0 and nums[i] == nums[i-1]:
continue
left = i + 1
right = len(nums) - 1
while left < right:
if nums[i] + nums[left] + nums[right] < 0:
left += 1
elif nums[i] + nums[left] + nums[right] > 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
Leetcode18. 四数之和
题目链接:18. 四数之和
C++:
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector<int>> results;
for(int i = 0; i < nums.size(); i++)
{
if(target >= 0 && nums[i] > target)
break;
if(i > 0 && nums[i] == nums[i-1])
continue;
for(int j = i+1; j < nums.size(); j++)
{
if(target >= 0 && nums[i] + nums[j] > target)
break;
if(j > i+1 && nums[j] == nums[j-1])
continue;
int left = j+1;
int right = nums.size() - 1;
while(left < right)
{
//强制类型转换
if((long)nums[i] + nums[j] + nums[left] + nums[right] > target)
right--;
//强制类型转换
else if((long)nums[i] + nums[j] + nums[left] + nums[right] < target)
left++;
else
{
results.push_back(vector<int> {nums[i], nums[j], nums[left], nums[right]});
while(left < right && nums[left] == nums[left+1])
left++;
while(left < right && nums[right] == nums[right-1])
right--;
left++;
right--;
}
}
}
}
return results;
}
};
Python:
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
result = []
nums.sort()
for k in range(len(nums)):
if nums[k] > target and target > 0:
break
if k > 0 and nums[k] == nums[k-1]:
continue
for i in range(k+1, len(nums)):
if nums[k] + nums[i] > target and target > 0:
break
if i > k+1 and nums[i] == nums[i-1]:
continue
left = i + 1
right = len(nums) - 1
while left < right:
if nums[k] + nums[i] + nums[left] + nums[right] > target:
right -= 1
elif nums[k] + nums[i] + nums[left] + nums[right] < target:
left += 1
else:
result.append([nums[k], 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