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

题目和15题的要求几乎一样,只不过是4Sum.

思路和15题一模一样,但仍要注意去重.

自己思路:

有了3sum的经验这个就好做了,思想和3sum没有区别

先排序;

固定i, 固定j, move lo and hi;

We'll come across 3 cases(sum == ta, sum > ta, sum < ta) and deal with them.

自己代码:

\(O(n^3)\) time, \(O(1)\) extra space.

vector<vector<int>> fourSum(vector<int>& A, int ta) {
const int n = A.size();
vector<vector<int>> res;
sort(A.begin(), A.end()); // 有了3sum的经验这个就好做了,思想和3sum没有区别
// 固定i, 固定j, move lo and hi
// we'll come across 3 cases(sum==ta, sum>ta, sum<ta) and deal with them
for (int i = 0; i < n - 3; i++) {
if (i > 0 && A[i] == A[i - 1]) continue; //去重
for (int j = i + 1; j < n - 2; j++) {
if ((j > i + 1) && (A[j] == A[j - 1])) continue; //去重
int lo = j + 1, hi = n - 1; while (lo < hi) {// lo <= hi 不可以
int sum = A[i] + A[j] + A[lo] + A[hi];
if (sum == ta) { res.push_back({A[i], A[j], A[lo], A[hi]}); while (lo < hi && A[hi] == A[hi - 1]) hi--; //去重
while (lo < hi && A[lo] == A[lo + 1]) lo++; //去重
lo++; // 只有lo++或 只有hi--也可 他俩都没有就无限循环了
hi--; } else if (sum > ta) {
while (lo < hi && A[hi] == A[hi - 1]) hi--; //去重
hi--;
} else { // sum < ta
while (lo < hi && A[lo] == A[lo + 1]) lo++; //去重
lo++;
}
}
}
}
return res;
}

18. 4Sum(中等)的更多相关文章

  1. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  3. 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 ...

  4. 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 ...

  5. 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 = ...

  6. [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 ...

  7. 18. 4Sum (JAVA)

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

  8. LeetCode——18. 4Sum

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

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

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

随机推荐

  1. eclipse版本对应的jdk版本

    Installing Eclipse is relatively easy, but does involve a few steps and software from at least two d ...

  2. LDAP apacheds解决方案

    Apache DS 配置与管理   LADP基本介绍 LDAP(轻量级目录访问协议)以目录的形式来管理资源(域用户,用户组,地址簿,邮件用户,打印机等等).   特点: 1. LDAP是一种网略协议而 ...

  3. python入门(7)Python程序的风格

    python入门(7)Python程序的风格 Python采用缩进方式,写出来的代码就像下面的样子: # print absolute value of an integer: a = 100 if ...

  4. 2.x与3.x差异、条件语句、数据类型、其他

    一.输入(raw_input)=====>python2.x版本 #!/usr/bin/env python # -*- coding: utf-8 -*- # 将用户输入的内容赋值给 name ...

  5. C#实现导出Excel

    这段时间用到了导出Excel的功能,这个功能还是比较常用的,我常用的有两个方法,现在整理一下,方便以后查看. 一.实现DataTable数据导出到本地,需要自己传进去导出的路径. /// <su ...

  6. 实现GridControl的行单元格非顺序跳转

    用GridControl控件添加数据的时候发现,有一些字段过多但是并不是每个字段都需要用户输入,每个单元格都回车跳转的时候不仅浪费时间,而且用户体验也不好,就需要单元格跳转的时候,不需要的字段可以隔过 ...

  7. CentOS7为firewalld添加开放端口

    运行.停止.禁用firewalld 启动:# systemctl start  firewalld 查看状态:# systemctl status firewalld 或者 firewall-cmd ...

  8. 如何在pycharm中使用配置好的virtualenv环境

    1.手动建立: 第一步 建立虚拟环境 Windows cmd: pip install virtualenv 创建虚拟环境目录 env 激活虚拟环境 C:\Python27\Scripts\env\S ...

  9. Spring(五):Spring&Struts2&Hibernate整合后,实现查询Employee信息

    背景: 基于之前两篇文章<Spring(三):Spring整合Hibernate>.<Spring(四):Spring整合Hibernate,之后整合Struts2>,了解了如 ...

  10. hdu4864 Task贪心好题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4864 题目大意: 有n个机器,m个任务.每个机器至多能完成一个任务.对于每个机器,有一个最大运行时间 ...