18. 4Sum -- 找到数组中和为target的4个数
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) {
int l = nums.size(), sum, left, right, i, j;
vector<vector<int>> ans;
if (l<)
return ans;
sort(nums.begin(), nums.end());
for (i = ; i<l - ; i++)
{
if(i && nums[i] == nums[i-])
continue;
for (j = i + ; j<l - ; j++)
{
if(nums[i]+nums[j]+nums[j+]+nums[j+]>target)
break;
if(nums[i]+nums[j]+nums[l-]+nums[l-]<target)
continue;
if (j>i + && nums[j] == nums[j - ])
continue;
sum = nums[i] + nums[j];
left = j + ;
right = l - ;
while (left < right)
{
if (sum + nums[left] + nums[right] == target)
ans.push_back({ nums[i], nums[j], nums[left++], nums[right--] });
else if (sum + nums[left] + nums[right] < target)
left++;
else
right--;
while (left>j+ && nums[left] == nums[left - ])
left++;
while(right<l- && nums[right] == nums[right + ])
right--;
}
}
}
return ans;
}
};
18. 4Sum -- 找到数组中和为target的4个数的更多相关文章
- (回溯法)数组中和为S的N个数
Given a list of numbers, find the number of tuples of size N that add to S. for example in the list ...
- 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9
python解决方案 nums = [1,2,3,4,5,6] #假如这是给定的数组 target = 9 #假如这是给定的目标值 num_list = [] #用来装结果的容器 def run(nu ...
- 【算法习题】正整数数组中和为sum的任意个数的组合数
1.递归实现(参考:https://blog.csdn.net/hit_lk/article/details/53967627) public class Test { @org.junit.Test ...
- Two sum(给定一个无重复数组和目标值,查找数组中和为目标值的两个数,并输出其下标)
示例: nums = [1,2,5,7] target = [6] return [0,2] Python解决方案1: def twoSum(nums, target): ""&q ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- 1. Two Sum&&15. 3Sum&&18. 4Sum
题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
随机推荐
- 常用的jquery
获取一组radio被选中项的值 var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本 var item = $(&qu ...
- main函数中argc理解
其实: int main(int argc,char *argv[])是UNIX和Linux中的标准写法,而int main()只是UNIX及Linux默许的用法..void main(int arg ...
- QQLogin
import java.awt.*; import javax.swing.*; public class QQLogin extends JFrame{ QQLogin(){ this.setSiz ...
- Dolphin for Android(v11.5.1[Jetpack:内置])
1. 下载的地址为“http://www.techspot.com/downloads/5927-dolphin-browser-for-android.html” ZC: 由于 Google Pla ...
- parseInt 的第二个参数
["1","2","3"].map(parseInt) //[1,NaN,NaN] ["1","2" ...
- jsonp实现原理详细介绍
主要是浏览器的同源同域(协议相同,域名相同及端口相同)策略需要使用跨域获取数据,故需要jsonp跨域获取数据.重点:img的src,link的href及script的src不遵循浏览器的同源同域策略, ...
- 【Todo】Lucene系统学习
之前已经写过一篇关于Lucene安装学习的文章:http://www.cnblogs.com/charlesblc/p/5980525.html 还有一篇关于Solr安装使用的文章:http://ww ...
- WDS的原理
WDS的原理 WDS 即 Wireless Distribution System,是无线网络部署延展系统的简称,指用多个无线网络相互联结的方式构成一个整体的无线网络.简单地说,WDS就是利用两个(或 ...
- 用Volley让GridView加载网络图片
一.布局文件 总共两个布局文件,一个是GridView,还有一个是GridView的item,是NetworkImageView和TextView activity_main.xml <Rela ...
- uva 11800 Determine the Shape
vjudge上题目链接:Determine the Shape 第二道独自 A 出的计算几何水题,题意就是给你四个点,让你判断它是哪种四边形:正方形.矩形.菱形.平行四边形.梯形 or 普通四边形. ...