【LeetCode】leetcode sum问题汇总小结
来源:互联网 发布:网络拓扑画图软件 编辑:程序博客网 时间:2024/06/01 09:11
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> rst; unordered_map<int,int> hash; for(int i=0;i<nums.size();i++) { int n=target-nums[i]; if(hash.find(n)!=hash.end()) { rst.push_back(hash[target-nums[i]]); rst.push_back(i); return rst; }else hash[nums[i]]=i; } return rst; }};2. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]这题结果可能有重复,并且不要求输出下标,所以可以先排序,再每次固定第一个数,对后两个数用two pointer。front back
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> rst; sort(nums.begin(),nums.end()); for(int i=0;i<nums.size();i++) { if(i>0&&nums[i]==nums[i-1]) continue; //跳过跟n1重复的 int n1=nums[i]; int front=i+1; int back=nums.size()-1; while(front<back) { n2=nums[front]; n3=nums[back]; if(n1+n2+n3<0) front++; else if(n1+n2+n3>0) back--; else{ vector<int> tmp{n1,n2,n3}; rst.push_back(tmp); while(front<back&&nums[front]==n2) front++; //跳过跟n2重复的 while(front<back&&nums[back]==n3) back--; //跳过跟n3重复的 } } } return rst; }};
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).这题跟上题类似,不过答案只有一个,且每次要判断是不是最近的点,求什么设什么。
class Solution {public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { int rst=0,len=A.size(); unordered_map<int,int> hasha,hashb,hashc,hashd; sort(A.begin(),A.end()); sort(B.begin(),B.end()); sort(C.begin(),C.end()); sort(D.begin(),D.end()); inithash(A,hasha); inithash(B,hashb); inithash(C,hashc); inithash(D,hashd); if(A[0]+B[0]+C[0]+D[0]>0) return 0; if(A[len-1]+B[len-1]+C[len-1]+D[len-1]<0) return 0; for(int i=0;i<len;i++) { if(i>0&&A[i]==A[i-1]) continue; int sum3=0-A[i]; for(int j=0;j<len;j++){ if(j>0&&A[j]==A[j-1]) continue; if(B[j]+C[j]+D[j]>0) break; if(B[len-1]+B[len-1]+C[len-1]<0) } } } void inithash(vector<int>&A, unordered_map<int,int> &hash){ for(int i=0;i<A.size();i++){ if(hash.find(a)==hash.end()) hash[a]=1; else hash[a]++; } }};
18. 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: 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>> total; int n = nums.size(); if(n<4) return total; sort(nums.begin(),nums.end()); for(int i=0;i<n-3;i++) { if(i>0&&nums[i]==nums[i-1]) continue; if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break; if(nums[i]+nums[n-3]+nums[n-2]+nums[n-1]<target) continue; for(int j=i+1;j<n-2;j++) { if(j>i+1&&nums[j]==nums[j-1]) continue; if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break; if(nums[i]+nums[j]+nums[n-2]+nums[n-1]<target) continue; int left=j+1,right=n-1; while(left<right){ int sum=nums[left]+nums[right]+nums[i]+nums[j]; if(sum<target) left++; else if(sum>target) right--; else{ total.push_back(vector<int>{nums[i],nums[j],nums[left],nums[right]}); do{left++;}while(nums[left]==nums[left-1]&&left<right); do{right--;}while(nums[right]==nums[right+1]&&left<right); } } } } return total; }};
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l)
there are such that A[i] + B[j] + C[k] + D[l]
is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
class Solution {public: void fillMap(vector<int>& A, vector<int>& B, unordered_map<int,int> &m) { int n = A.size(); for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) ++m[A[i] + B[j]]; } int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { unordered_map<int,int> m1, m2; fillMap(A, B, m1); fillMap(C, D, m2); int res = 0; for(auto it = m1.begin(); it != m1.end(); ++it) { auto it2 = m2.find(-it->first); if(it2 != m2.end()) res += it->second*it2->second; } return res; }
- 【LeetCode】leetcode sum问题汇总小结
- [Leetcode][求和问题2Sum/3Sum/4Sum/KSum]相关题目汇总/分析/总结
- leetcode-4sum问题
- leetcode:3Sum问题
- leetcode sum问题
- leetcode 3sum问题
- LeetCode two sum问题
- leetcode 回文问题小结
- [LeetCode] 2Sum, 3Sum, 4Sum小结
- leetcode OJ1 Two Sum求解小结
- Leetcode #1 Two Sum 解题小结
- LeetCode题解 || Two Sum问题
- leetcode 题解 || 3Sum 问题
- [c++] LeetCode Two Sum问题
- 【Leetcode】之Two Sum问题
- 【Leetcode】之3sum问题
- leetcode之n Sum 问题
- leetcode--ksum问题--3sum
- instanceof, isinstance,isAssignableFrom的区别
- 通讯录之按汉字首字母排序-java
- BZOJ 2733: [HNOI2012]永无乡 (Treap+启发式合并)
- Java 父类与子类 类型转换
- 微信小程序入门-弹性模型
- 【LeetCode】leetcode sum问题汇总小结
- VS Code折腾记 - (2) 快捷键大全,没有更全
- Wa-UI
- Spring学习10-- AOP实现原理
- 中介者模式---论媒婆的重要性
- c语言初级阶段散知识整理(一)
- Struts1.3——DispatchAction、DynamicForm和全局跳转
- 以目标为导向的需求工程建模方法:KAOS
- 阿里云服务器升级nginx 到最新稳定版