时长:4小时
454. 四数相加II
Given four integer arrays nums1 , nums2 , nums3 , and nums4 all of length n , return the number of tuples (i, j, k, l) such that:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Example 1:
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] Output: 1
Constraints:
n == nums1.length
n == nums2.length
n == nums3.length
n == nums4.length
1 <= n <= 200
-2 28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2 28
解题思路:
四个数两辆分组:
先求A,B中特定的和(key)到组成和的不重复对的数目(value)
在C、D中同样操作
在C&D的map中找A&B的元素中的对应值,并将两个value相乘加到count上
需要注意的要点:
数组巧妙循环:
for (int a : A) {
...
}
// 我的实现
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int, int> umap12;
unordered_map<int, int> umap34;
int sum;
for(int i = 0; i < nums1.size(); ++ i) {
for(int j = 0; j < nums2.size(); ++ j) {
sum = nums1[i] + nums2[j];
auto it = umap12.find(sum);
if (it == umap12.end()) {
umap12[sum] = 1;
}
else {
umap12[sum] ++;
}
}
}
for(int i = 0; i < nums3.size(); ++ i) {
for(int j = 0; j < nums4.size(); ++ j) {
sum = nums3[i] + nums4[j];
auto it = umap34.find(sum);
if (it == umap34.end()) {
umap34[sum] = 1;
}
else {
umap34[sum] ++;
}
}
}
int count = 0;
auto it=umap12.begin();
for(; it!=umap12.end(); it ++) {
auto it_tmp = umap34.find(it->first*(-1));
if (it_tmp != umap34.end()) {
count += (it->second * it_tmp->second);
}
}
// for(auto it=umap12.begin(); it != umap12.end(); it ++) {
// cout << it->first << "\t";
// for(int i = 0; i < it->second.size(); ++ i) {
// cout << it->second[i][0] << " " << it->second[i][1] << "|";
// }
// cout << endl;
// }
// cout << "---------------------------------" << endl;
// for(auto it=umap34.begin(); it != umap34.end(); it ++) {
// cout << it->first << "\t";
// for(int i = 0; i < it->second.size(); ++ i) {
// cout << it->second[i][0] << " " << it->second[i][1] << "|";
// }
// cout << endl;
// }
return count;
}
};
// carl
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> umap; //key:a+b的数值,value:a+b数值出现的次数
// 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中
for (int a : A) {
for (int b : B) {
umap[a + b]++;
}
}
int count = 0; // 统计a+b+c+d = 0 出现的次数
// 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
for (int c : C) {
for (int d : D) {
if (umap.find(0 - (c + d)) != umap.end()) {
count += umap[0 - (c + d)];
}
}
}
return count;
}
};
383. 赎金信
Given two strings ransomNote and magazine , return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote .
Example 1:
Input: ransomNote = "a", magazine = "b" Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab" Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab" Output: true
Constraints:
1 <= ransomNote.length, magazine.length <= 10 5
ransomNote and magazine consist of lowercase English letters.
解题思路:
HashMap对Magazine中元素计数
变量Ransom中的元素,在HashMap中减去相应值,出现小于0的元素则false
否则true
需要注意的要点:
无
// 我的实现
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> umap;
for(int i = 0; i < magazine.length(); ++ i) {
auto it = umap.find(magazine[i]);
if(it == umap.end()) {
umap[magazine[i]] = 1;
}
else {
umap[magazine[i]] ++;
}
}
for(int i = 0; i < ransomNote.length(); ++ i) {
auto it = umap.find(ransomNote[i]);
if(it == umap.end()) {
return false;
}
else {
if(--umap[ransomNote[i]] < 0) {
return false;
}
}
}
return true;
}
};
// carl
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int record[26] = {0};
//add
if (ransomNote.size() > magazine.size()) {
return false;
}
for (int i = 0; i < magazine.length(); i++) {
// 通过recode数据记录 magazine里各个字符出现次数
record[magazine[i]-'a'] ++;
}
for (int j = 0; j < ransomNote.length(); j++) {
// 遍历ransomNote,在record里对应的字符个数做--操作
record[ransomNote[j]-'a']--;
// 如果小于零说明ransomNote里出现的字符,magazine没有
if(record[ransomNote[j]-'a'] < 0) {
return false;
}
}
return true;
}
};
15. 三数之和
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j , i != k , and j != k , and nums[i] + nums[j] + nums[k] == 0 .
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0.
Constraints:
3 <= nums.length <= 3000
-10 5 <= nums[i] <= 10 5
解题思路:
先排序
外面一重循环遍历第一个数(left)
mid和right作为两个指针
和大了,right--
和小了,mid++
满足条件,循环mid++,直到指向与mid-1不同的值;循环right--直到指向与right+1 不同的值
更新left的时候也是,注意也要让left更新到一个与left-1不同的值
需要注意的要点:
由于三者加和为0,若排序后的nums[left] > 0,则可以直接break;
// 我的实现
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int mid, right, last;
vector<vector<int>> res;
for(int left = 0; left < nums.size()-2; ++ left) {
if (left > 0 && nums[left] == last) {
continue;
}
mid= left + 1;
right = nums.size()-1;
while(mid < right) {
// cout << "left:" << left << "mid:" << mid << "right:" << right << "\t";
// cout << "nums[left]:" << nums[left] << "nums[mid]:" << nums[mid] << "nums[right]:" << nums[right] << endl;
if(nums[left] + nums[mid] + nums[right] > 0) {
right --;
}
else if(nums[left] + nums[mid] + nums[right] < 0) {
mid ++;
}
else {
vector<int> tmp = {nums[left], nums[mid], nums[right]};
res.emplace_back(tmp);
int idx = mid;
while(mid < right && nums[idx] == nums[mid]) {
mid ++;
}
idx = right;
while(right > mid && nums[idx] == nums[right]) {
right --;
}
}
}
last = nums[left];
}
return res;
}
};
// carl
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
// 找出a + b + c = 0
// a = nums[i], b = nums[left], c = nums[right]
for (int i = 0; i < nums.size(); i++) {
// 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了
if (nums[i] > 0) {
return result;
}
// 错误去重a方法,将会漏掉-1,-1,2 这种情况
/*
if (nums[i] == nums[i + 1]) {
continue;
}
*/
// 正确去重a方法
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (right > left) {
// 去重复逻辑如果放在这里,0,0,0 的情况,可能直接导致 right<=left 了,从而漏掉了 0,0,0 这种三元组
/*
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
*/
if (nums[i] + nums[left] + nums[right] > 0) right--;
else if (nums[i] + nums[left] + nums[right] < 0) left++;
else {
result.push_back(vector<int>{nums[i], nums[left], nums[right]});
// 去重逻辑应该放在找到一个三元组之后,对b 和 c去重
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
// 找到答案时,双指针同时收缩
right--;
left++;
}
}
}
return result;
}
};
18. 四数之和
Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0 Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8 Output: [[2,2,2,2]]
Constraints:
1 <= nums.length <= 200
-10 9 <= nums[i] <= 10 9
-10 9 <= target <= 10 9
解题思路:
先排序
在三数之和的基础上再套一层循环,作为第一个数i,来找和为target-nums[i]的三个数
i的更新也要让nums[i] != nums[i-1]为止
这里不能用nums[i] > target来break,因为target可能为负数,例如target是-5, 数为{-4, -1},就会出错,要判断nums[i] > 0 && nums[i] > target 则break;
再内层循环也是同理,但target改成target-nums[i]
需要注意的要点:
内层三个数加起来可能溢出,转换为long
vector<vector<int>> 的push
result.push_back(vector<int>{nums[k], nums[i], nums[left], nums[right]})
哈希表总结
unordered_map,存key->value
unordered_set,存key
上面两者比较常用。
vector转unordered_set
unordered_set a(vec.begin(), vec.end());
声明:
文章中LeetCode的题目来自LeetCode官网:https://leetcode.cn/problems