LeetCode(18):四数之和
Medium!
题目描述:
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例:
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
解题思路:
这里为了避免重复项,我们使用了STL中的set,其特点是不能有重复,如果新加入的数在set中原本就存在的话,插入操作就会失败,这样能很好的避免的重复项的存在。此题的O(n3)解法的思路跟三数之和基本没什么区别,就是多加了一层for循环,其他的都一样,代码如下:
C++参考答案:
class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
set<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = ; i < int(nums.size() - ); ++i) {
for (int j = i + ; j < int(nums.size() - ); ++j) {
if (j > i + && nums[j] == nums[j - ]) continue;
int left = j + , right = nums.size() - ;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
vector<int> out{nums[i], nums[j], nums[left], nums[right]};
res.insert(out);
++left; --right;
} else if (sum < target) ++left;
else --right;
}
}
}
return vector<vector<int>>(res.begin(), res.end());
}
};
LeetCode(18):四数之和的更多相关文章
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
- [Leetcode 18]四数之和 4 Sum
[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 【LeetCode】四数之和
[问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...
- [LeetCode] 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 ...
- 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
随机推荐
- CentOS75 安装Oracle18c
1. 参考地址 https://blog.csdn.net/u010257584/article/details/50902472https://www.cnblogs.com/kerrycode/a ...
- PHP阿里云云解析签名, 通过API 绑定域名到动态 ip
<?php date_default_timezone_set("GMT"); //绑定 ip 到域名 Ali::Obj()->UpdateDomainRecord() ...
- Caffe使用: Ubuntu 14.04(x64) 从cuda 7.0 升级到 cuda8.0
由于之前已经在Ubuntu 14.04 x64上面安装cuda7.0+caffe, 并且已经配置好,caffe也已经跑通. 但是最近需要使用Torch,而Torch对cuda的要求是8.0,因此决定对 ...
- 浅谈final修饰的变量
一直大概的知道final关键字的作用,但是自己实际工作中却很少用,除非在声明一些常量值的时候,今天忽然自己在项目中用一个map进行存储一些值.一开始我只是用private修饰的,心里想的是如果fina ...
- browser-sync & http server
browser-sync & http server browser-sync https://www.browsersync.io/ usage # step 1 $ npm install ...
- oralce下载
oracle的官网网址:oracle.com 打开之后选择中文 然后在页面的下方找到下载和试用链接 点击进入,选择数据库下载,在页面中找到如下部分点击进入即可下载对应版本的oracle
- BZOJ4066 简单题(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 开发Spring Shell应用程序
2 开发Spring Shell应用程序 向shell提供命令非常简单,需要学习的注解很少.该命令的实现风格与使用依赖注入的应用程序的开发类相同,您可以利用Spring容器的所有特性来实现您的命令类. ...
- M公司的回忆录——L公司
2013年3月笔者回国的第一站便是这人称有福之州的福州.受我兄弟之邀笔者来到了L公司.而这也是笔者与M公司相遇的前缘.此时的L公司只是一个刚起步的创业公司.与所有创业公司一样子——创业初期存在着很多问 ...
- Eclipse中创建java类的时候自动设置作者信息和创建时间
window—>preferences—>Java—>Code Stype—>Code Templates Code—>New Java files 点击Edit ${f ...