代码随想录【Day 7】 | 454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
454.四数相加II
题目链接:454.四数相加II
卡尔文字讲解
解题思路重点:
- 这道题目是四个独立的数组,只要找到A[i] + B[j] + C[k] + D[l] = 0就可以,不用考虑有重复的四个元素相加等于0的情况,所以相对于题目18. 四数之和,题目15.三数之和,还是简单了不少!
- 两个数组为一组相加, 然后寻找剩下的两个数组的合 是否是第一组两个数组合的( 0 - (a+b))。
代码实现:
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int, int> chk_map;
int ret = 0;
for (auto num1 : nums1 ){
for (auto num2 : nums2 ){
chk_map[num1 + num2]++;
}
}
for (auto num3 : nums3 ){
for (auto num4 : nums4 ){
if ( chk_map.find( 0 - (num3 + num4)) != chk_map.end())
ret += chk_map[ 0 - (num3 + num4) ];
}
}
return ret;
}
};
383. 赎金信
题目链接:349.两个数组的交集
卡尔文解
解题思路及注意事项:
- 为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思” 这里说明杂志里面的字母不可重复使用。
- 你可以假设两个字符串均只含有小写字母。 说明只有小写字母,这一点很重要。
代码实现:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int magazine_alphabet[26] = {0};
for ( int idx = 0; idx < magazine.size(); idx++ ){
magazine_alphabet[ magazine[idx] - 'a' ]++;
}
for ( int idx = 0; idx < ransomNote.size(); idx++ ){
magazine_alphabet[ ransomNote[idx] - 'a' ]--;
}
for ( int idx = 0; idx < 26; idx++ ){
if ( magazine_alphabet[idx] < 0 )
return false;
}
return true;
}
};
15. 三数之和
题目链接:15.三数之和
卡尔文解
解题思路及注意事项:
- 先排序后,再使用 idx 外加 left & right 双指针法。
- 注意 最外层的 idx 指针的去重法。
- 注意 for loop 里面的 left 跟 right 指针的去重法。
代码实现:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ret;
sort(nums.begin(), nums.end());
for ( int idx = 0; idx < nums.size(); idx++ ){
//Immidately return result if the first element > 0 after sorting it up.
if ( nums[idx] > 0 )
return ret;
// a + b + c = 0;
// Remove duplicate a;
if ( idx > 0 && nums[idx] == nums[idx - 1])
continue;
int left = idx + 1;
int right = nums.size() - 1;
while ( left < right )
{
if ( nums[idx] + nums[left] + nums[right] > 0 ) right--;
else if ( nums[idx] + nums[left] + nums[right] < 0 ) left++;
else{
ret.push_back( vector<int>{nums[idx], nums[left], nums[right]} );
//Remove duplicate left and right
//For instance : -1 -1 -1 -1 -1 -1 1 1 1 1 1 2 2 2
while ( left < right && ( nums[left] == nums[left + 1] )) left++;
while ( left < right && ( nums[right] == nums[right - 1] )) right--;
//Ajust left and right simultaneously , when find satisfied numbers
left++;
right--;
}
}
}
return ret;
}
};
18. 四数之和
题目链接:18.四数之和
卡尔文解
解题思路及注意事项:
1.四数之和,和15.三数之和 (opens new window)是一个思路,都是使用双指针法, 基本解法就是在15.三数之和 (opens new window)的基础上再套一层for循环。
2. 但是有一些细节需要注意,例如: 不要判断nums[k] > target 就返回了,三数之和 可以通过 nums[i] > 0 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是[-4, -3, -2, -1],target是-10,不能因为-4 > -10而跳过。但是我们依旧可以去做剪枝,逻辑变成nums[i] > target && (nums[i] >=0 || target >= 0)就可以了。
3. 注意 for loop 里面的 left 跟 right 指针的去重法。
代码实现:
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector<int>> ret;
for ( int k = 0; k < nums.size(); k++ ){
//第一层剪枝
if (( nums[ k ] >= 0) && nums[ k ] > target )
break;
//第一层去重
if (( k > 0 ) && ( nums[ k ] == nums[ k - 1 ]))
continue;
for ( int i = k + 1; i < nums.size(); i++ ){
//第二层剪枝
if ((( nums[ k ] + nums[ i ] ) >= 0 ) && (( nums[ k ] + nums[ i ] ) > target ))
break;
//第二层去重
if (( i > k + 1) && nums[ i ] == nums[ i - 1 ])
continue;
int left = i + 1;
int right = nums.size() - 1;
while ( left < right ){
if (( (long) nums[ k ] + nums[ i ] + nums[ left ] + nums[ right ]) > target ){
right--;
}
else if (( (long) nums[ k ] + nums[ i ] + nums[ left ] + nums[ right ]) < target ){
left++;
}
else{
//找到目标位置,生成数值vector, 加入到返回二维数组
ret.push_back(vector<int>{ nums[ k ], nums[ i ], nums[ left ], nums[ right ]});
//对于 left 跟 right 指针所指元素去重
while( left < right && ( nums[ left ] == nums[ left + 1 ])) left++;
while( left < right && ( nums[ right ] == nums[ right - 1 ])) right--;
//双指针同时shrink,在找到相应位置时
left++;
right--;
}
}
}
}
return ret;
}
};