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 相等?找出所有满 ...
随机推荐
- Android 获取加速传感器的值,并去除杂音
1.注册和注销传感器 private void registerSensor() { manager.registerListener(this, manager.getDefaultSensor(S ...
- Eclipse中项目上有小红叉,但就是找不到报错文件(总结,持续更新)
1.jdk问题解决:jdk配置参考:http://blog.csdn.net/superit401/article/details/72847110 2.build path:项目右键——Build ...
- P2824 [HEOI2016/TJOI2016]排序
题面 这是一道非常巧妙的线段树的题 我们会发现维护\(1 \sim n\)的序列非常困难,但如果我们维护\(01\)序列的的顺序,就非常容易了 但是我们怎么能把这道题变成维护\(01\)序列的顺序呢? ...
- Django_博客项目 注册用户引发 ValueError: The given username must be set
博客项目中 注册功能在ajax 提交数据时 报错 ValueError: The given username must be set 锁定到错误点为 判定为是无法获取到 username 字段 那先 ...
- 洛谷 P1309 瑞士轮 解题报告
P1309 瑞士轮 题目背景 在双人对决的竞技性比赛,如乒乓球.羽毛球.国际象棋中,最常见的赛制是淘汰赛和循环赛.前者的特点是比赛场数少,每场都紧张刺激,但偶然性较高.后者的特点是较为公平,偶然性较低 ...
- 【bzoj1492】 NOI2007—货币兑换Cash
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 (题目链接) 题意 两种金券,金券按照比例交易:买入时,将投入的资金购买比例为$rate[i] ...
- 【bzoj4012】 HNOI2015—开店
http://www.lydsy.com/JudgeOnline/problem.php?id=4012 (题目链接) 题意 一棵树,每条边有正边权,每个点的点权非负.若干组询问,强制在线,每次查询点 ...
- 【CH4201】楼兰图腾
题目大意:给定一个长度为 N 的序列,从序列中任意挑出三个数,求满足中间的数字值最小(最大)有多少种情况. 题解:建立在值域上的树状数组,从左到右扫描一遍序列,统计出每个点左边有多少个数大于(小于)该 ...
- linux文件名匹配——通配符使用
背景:在linux使用过程中,经常需要查找文件,对命令中的通配符pattern和正则表达式的区分不是很清楚.有必要好好研究一下. 1 扫盲 1.1 通配符和正则表达式 当在使用命令行时,有很多时间都用 ...
- RabbitMQ的生产者和消费者
低级错误:启动程序的时候报错:socket close: 原因在配置文件中写的端口是:15672,应该是5672: client端通信口5672管理口15672server间内部通信口25672erl ...