15. 3Sum 和 18. 4Sum
来源:互联网 发布:三星ssd数据迁移 编辑:程序博客网 时间:2024/05/02 13:37
3sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)
主要思想和2sum一样,固定第一个数,然后用2sum同样的方法去找另外两个数。
需要注意的是,为了去掉重复的,需要跳过已经用过的数字
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> result; if(nums.size() < 3) return result; sort(nums.begin(), nums.end()); for(int i = 0; i < nums.size();i++){ int target = -nums[i]; int l = i+1,r = nums.size()-1; while(l < r) { int sum = nums[l] + nums[r]; if( sum < target) l++; else if(sum > target) r--; else{ vector<int> res(3); res[0] = nums[i]; res[1] = nums[l]; res[2] = nums[r]; result.push_back(res); while(l < r && nums[l] == res[1]) l++; //用过的数字跳过 while(l < r && nums[r] == res[2]) r--; } } while(i+1< nums.size() && nums[i] == nums[i+1]) i++; //用过的数字跳过 } return result; }};
4sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)
class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> result; if(nums.size() <4) return result; sort(nums.begin(), nums.end()); for(int i = 0; i < nums.size();i++){ int target1 = target - nums[i]; for(int j = i+1;j < nums.size();j++){ int target2 = target1 - nums[j]; int l = j+1, r= nums.size()-1; while(l < r){ int sum = nums[l] + nums[r]; if(sum < target2) l++; else if (sum > target2) r--; else { vector<int> res(4); res[0] = nums[i]; res[1] = nums[j]; res[2] = nums[l]; res[3] = nums[r]; result.push_back(res); while(l < r && nums[l] == res[2]) l++; while(l < r && nums[r] == res[3]) r--; } } while(j+1 < nums.size() && nums[j] == nums[j+1]) j++; } while(i+1 < nums.size() && nums[i] == nums[i+1]) i++; } return result; }};
- 15. 3Sum 和 18. 4Sum
- Leetcode 1. Two Sum & 15. 3Sum & 16. 3Sum Closest & 18. 4Sum
- (java)求3sum和4sum
- 3sum和4sum(two pointers)
- 15. 3Sum && 16 sum closet && 18 4sum
- 15. 3Sum,16. 3Sum Closest,18. 4Sum(最后一个方法重要)重要
- 【C++】【LeetCode】15. 3Sum & 16. 3Sum Closest & 18. 4Sum
- leetcode 15. 3sum && 16. 3Sum Closest && 18. 4Sum
- 3Sum 3Sum Closest 4Sum
- 3Sum & 3Sum Closest & 4Sum
- 3sum、3Sum closet、 4sum
- 3Sum, 3Sum Closest, 4 Sum
- leetcode 2 sum 3sum 4sum
- 2Sum 3Sum 4Sum
- Leetcode 2SUM-3SUM-4SUM
- Leetcode-2sum,3sum,4sum
- leetcode 2 sum & 3 sum & 4 sum
- [LeetCode] 2Sum, 3Sum, 4Sum, 3SUm closet
- 数据库优化的思路
- 检测是否通过点击通知来启动App
- Ubuntu下wine1.7升级到最新版
- Servlet
- Qt发布可能遇到的问题
- 15. 3Sum 和 18. 4Sum
- 网络革命:软件定义网络与网络虚拟化
- cocos2d-x·技巧]关于cocos2d-x在Android下实现单点触摸小技巧
- 背包问题之6
- socket简单整理
- uva 11729 —— Commando War
- uva 12124 —— Assemble
- uva 12097—— Pie
- uva 11292 ——The Dragon of Loowater