454.四数相加II 4 Sum II
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
可以理解为a+b+c+d = 0
先遍历a和b中和, 然后在c和d中寻找target = 0-(c+d) 因为a+b+c+d = 0
最后把寻找到的target的value加到count中就可以了
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;
int target;
for (int c : nums3){
for (int d : nums4){
target = 0 - (c + d);
if (map.find(target) != map.end()){
count += map[target];
}
}
}
return count;
}
};
383. 赎金信 Ramsom Note
比较简单,直接数组查里面字母出现的次数
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int ransom[26] = {0};
int mag[26] = {0};
for (int i = 0; i < ransomNote.size(); i++){
ransom[ransomNote[i] - 'a']++;
}
for (int i = 0; i < magazine.size(); i++){
mag[magazine[i] - 'a']++;
}
for (int i = 0; i < 26; i++){
if (ransom[i] > mag[i]){
return false;
}
}
return true;
}
};
15. 三数之和 3Sum
双指针法比哈希表做更好
先进行排序.
nums[1],nums[left],nums[right]
如果(nums[1]+nums[left],nums[right]) >0那么right--
如果(nums[1]+nums[left],nums[right]) <0那么left++
如果(nums[1]+nums[left],nums[right]) =0那么保存结果到result
需要对nums[1],nums[left],nums[right]进行去重.
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++){
if (nums[i] > 0){
return result;
}
if (i > 0 && nums[i] == nums[i - 1]){
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (right > left){
if (nums[i] + nums[left] + nums[right] > 0){
right--;
} else if (nums[i] + nums[left] + nums[right] < 0){
left++;
} else {
//cout << nums[i] << nums[left] << nums[right] << endl;
result.push_back(vector<int>{nums[i],nums[left],nums[right]});
while (right > left && nums[left] == nums[left + 1]){
left++;
}
while (right > left && nums[right] == nums[right - 1]){
right--;
}
right--;
left++;
}
}
}
return result;
}
};
18. 四数之和 4Sum
跟上一题三数之和的思路一样的,还是通过双指针来进行
主要注意去重的时候四个数都需要进行
需要在if判定里加上(long) 不然会溢出.
当剪枝的时候用break
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
sort(nums.begin(),nums.end());
for (int i = 0; i < nums.size(); i++){
if (nums[i] > target && (nums[i] >= 0 || target > 0)){
break;
}
if (i > 0 && nums[i] == nums[i - 1]){
continue;
}
for (int j = i + 1; j < nums.size(); j++){
if (nums[i] + nums[j] > target && (nums[i] + nums[j] >= 0 || target > 0)){
break;
}
if (j > i + 1 && nums[j] == nums[j - 1]){
continue;
}
int left = j + 1;
int right = nums.size() - 1;
while (right > left){
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 {
//cout << nums[i] << nums[j] << nums[left] << nums[right] << endl;
result.push_back(vector<int>{nums[i],nums[j],nums[left],nums[right]});
while (right > left && nums[right] == nums[right - 1]){
right--;
}
while (right > left && nums[left] == nums[left + 1]){
left++;
}
right--;
left++;
}
}
}
}
return result;
}
};