18. 4Sum -Medium

descrition

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]
]

解析

与 3Sum 的思路一样。不过代码量一大要 bug free 还真得细心一些!!

code


#include <iostream>
#include <vector>
#include <algorithm>
#include <limits> using namespace std; class Solution{
public:
int threeSumClosest(vector<int>& nums, int target){
sort(nums.begin(), nums.end()); // ascending int min_gab = numeric_limits<int>::max();
int ans = target; for(int i=0; i<nums.size(); i++){
int target_local = target - nums[i];
int ileft = i + 1;
int iright = nums.size() - 1;
while(ileft < iright){ // two pointer searching
int sum = nums[ileft] + nums[iright];
if(sum == target_local) // right answer
return target;
if(sum < target_local) // move ileft to increase sum
ileft++;
else // sum > target_local
iright--; int gab = abs(sum - target_local);
if(gab < min_gab){
ans = sum + nums[i];
min_gab = gab;
}
}
} return ans; }
}; int main()
{
return 0;
}

[array] leetCode-18. 4Sum -Medium的更多相关文章

  1. Array + two points leetcode.18 - 4Sum

    题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in num ...

  2. [LeetCode] 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 = tar ...

  3. [LeetCode] 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 = tar ...

  4. LeetCode 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 = tar ...

  5. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  6. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  7. Leetcode 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 = tar ...

  8. Java [leetcode 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 ...

  9. C#解leetcode 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 = tar ...

  10. [leetcode]18. 4Sum四数之和

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

随机推荐

  1. 【深入篇】Andorid中常用的控件及属性

    TextView  android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/al ...

  2. 博弈论 SG函数(模板) HDU 1848 Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. GDSOI2019划水记

    Day -9 北京集训结束,飞回广州浪两天后回校. Day -2 回家休整,打麻将技术进一步提高. Day 0 前往佛山入住酒店,论被人用大床房换双人房是什么体验??(一个人住真的舒服) 待在房间背模 ...

  4. mount---挂载文件系统

    挂载概念 Linux中的根目录以外的文件要想被访问,需要将其“关联”到根目录下的某个目录来实现,这种关联操作就是“挂载”,这个目录就是“挂载点”,解除次关联关系的过程称之为“卸载”. 注意:“挂载点” ...

  5. 7lession-基础数据使用介绍

    1.数值 这个使用比较简单 a = 1 b = 3.2 c = 12.5+4j d = 20L 2.字符串 代码 s = "hello world,i am comming" pr ...

  6. C#截取中英文混合字符串分行显示

    private int GetStrByteLength(string str) { return System.Text.Encoding.Default.GetByteCount(str); } ...

  7. Cts框架解析(1)-windows下cts配置

    环境搭建 下载 cts工具的下载地址:http://source.android.com/compatibility/downloads.html windows选择Android4.4 R3 Com ...

  8. Rotation--控件位置旋转

    今天想要完成一个按钮的动画,也就是随着手势在屏幕上的滑动,让按钮图片跟着旋转.刚开始的思路是,先把图片旋转以后,在把这个图片设置为imagebutton的背景.不过,会发现这个图片经过处理以后一直变形 ...

  9. Spark 概念学习系列之Spark 多语言编程

    不多说,直接上干货! Spark 同时支持Scala.Python.Java 三种应用程序API编程接口和编程方式, 考虑到大数据处理的特性,一般会优先使用Scala进行编程,其次是Python,最后 ...

  10. 方正飞越 A600硬改BIOS激活win7的工具与方法。

    硬件:方正飞越A600-4E57:主板,H61 IPISB-VR:BIOS版本,方正A007SB0(AMI) 软件:Win7专业版 目标:修改BIOS,添加SLIC2.1,硬激活win7 OEM版 具 ...