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. SpringBoot应用的启动方式

    一:IDE 运行Application这个类的main方法 二:在SpringBoot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...

  2. ELK学习总结(4-2)关于导入数据

    用REST API的_bulk来批量插入,可以达到5到10w条每秒 把数据写进json文件,然后再通过批处理,执行文件插入数据: 1.先定义一定格式的json文件,文件不能过大,过大会报错 2.后用c ...

  3. margin-top塌陷

    margin-top 塌陷 在两个不浮动的盒子嵌套时候,内部的盒子设置的margin-top会加到外边的盒子上,导致内部的盒子margin-top设置失败,解决方法如下: 1.外部盒子设置一个边框: ...

  4. Microsoft CRM-QueryExpression 成员

      名称 ColumnSet 获取或设置要包含的列. Criteria 获取或设置过滤查询结果的复杂条件和逻辑过滤器表达式. Distinct 获取或设置查询的结果是否包含重复的实体实例. Entit ...

  5. MySQL 5.7 基于复制线程SQL_Thread加快恢复的尝试

    1. MySQL 数据恢复常用办法 MySQL恢复的方法一般有三种: 1. 官方推荐的基于全备+binlog , 通常做法是先恢复最近一次的全备,然后通过mysqlbiinlog --start-po ...

  6. @OnetoOne @OnetoMany @ManyToOne(2)

    在班主任(id,name,bjid) 班级(id name) 学生(id name bjid)的 关系中 班主任一对一关联班级 班级一对多关联学生 @OnetoOne @joinColumn(bjid ...

  7. C++11 作用域内枚举

    enum class MyEnum{ P1 = , P2, P3, P4, P5 }; MyEnum myEnum = MyEnum::P2; 使用作用域的方式获取并限定P2的值.之所以要使用作用域, ...

  8. JavaScript的作用;JS常见的三种对话框;==和===的区别;函数内部参数数组arguments在函数内部打印实参;JS的误区:没有块级作用域

    JS:客户端(浏览器)脚本语言 弱类型 基于原型 事件驱动 不需要编译(直接运行)   JS的作用:表单验证,减轻服务端的压力 添加页面动画效果  动态更改页面内容  Ajax网络请求 (一)常见的对 ...

  9. dropzone.js使用实践

    官网地址:http://www.dropzonejs.com/ 一,它是什么: DropzoneJS is an open source library that provides drag'n'dr ...

  10. [ZJOI 2008]泡泡堂BNB

    Description 题库链接 双方 \(n\) 人,给出每人的战斗力,赢一场加 \(2\) 分,平局 \(1\) 分,失败不得分.求最大和最小的得分. \(1\leq n\leq 100000\) ...