Leetcode Week12
来源:互联网 发布:lol比赛视频软件 编辑:程序博客网 时间:2024/05/13 07:09
求4sum,原理同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: 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>>* v; vector<vector<int>> fourSum(vector<int>& num, int target) { v = new vector<vector<int>>(); if(num.size()<4) return *v; sort(num.begin(),num.end()); for(int i=0;i<num.size()-3;i++) { if(i>0&&num[i]==num[i-1]) continue; for(int j=i+1;j<num.size()-2;j++) { if(j>i+1&&num[j]==num[j-1]) continue; for(int k=j+1;k<num.size()-1;k++) { if(k>j+1&&num[k]==num[k-1]) continue; int left = k+1; int right = num.size()-1; while(left <= right) { int mid = left + (right-left)/2; if(num[i]+num[j]+num[k]+num[mid] == target) { vector<int> tmp; tmp.push_back(num[i]); tmp.push_back(num[j]); tmp.push_back(num[k]); tmp.push_back(num[mid]); v->push_back(tmp); break; }else if(num[i] + num[j] + num[k] + num[mid] > target) { right = mid-1; }else{ left = mid+1; } } } } } return *v; }};
0 0
- Leetcode Week12
- leetcode week12
- LeetCode题解 week12
- LeetCode#654 Maximum Binary Tree (week12)
- week12-leetcode #347-Top-K-Frequent-Elements
- Leetcode: 39. Combination Sum(Week12, Medium)
- 算法设计Week12 LeetCode Algorithms Problem #62 Unique Paths
- 算法设计Week12 LeetCode Algorithms Problem #63 Unique Paths II
- LeetCode Week12: Best Time to Buy and Sell Stock系列
- [LeetCode-Algorithms-49] "Group Anagrams" (2017.11.21-WEEK12)
- [LeetCode-Algorithms-62] "Unique Paths" (2017.11.21-WEEK12)
- Algorithm-week12
- week12晚上练习
- MultiAbnormalit2(week12)
- sql server code week12
- C程序设计Week12晚上练习
- week12---5月19日
- week12---5月20日
- android app动图优化:源码giflib加载gif动图,性能秒杀glide
- Java基础学习之-02
- Java 输入外挂
- Parity check——打表找规律+大数取模
- 使用Swift 3.0实现原生的3DES加密和解密
- Leetcode Week12
- Java基础学习之-03
- AOP架构之路-AspectJ
- 初识mongodb----索引
- Android Toast
- 蓝桥杯 算法提高 学霸的迷宫(bfs+方向输出)
- MySQL 学习<一> MySQL简介
- MySql数据库索引原理
- Hibernate逆向工程An internal error occurred during: "Generating Artifacts". java.lang.NullPointerExcepti