leetcode18—4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums 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.
Example:
Given array nums = [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的解决方式,设立双指针求解
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int len = nums.size();
vector<vector<int>> result;
== len)
return result;
sort(nums.begin(),nums.end());
; i < len- ; i++){
&& nums.at(i) == nums.at(i-))
continue;
&& nums.at(i) + nums.at(i+)+nums.at(i+)+nums.at(i+) > target)
break;
&& nums.at(i) + nums.at(len-)+nums.at(len-)+nums.at(len-) < target)
continue;
; j < len- ; j++){
&& nums[j] == nums[j-])
continue;
&& nums[i] + nums[j] + nums[j+] + nums[j+] > target)
break;
&& nums[i] + nums[j] + nums[len-] + nums[len-] < target)
continue;
;
;
while(left < right){
int temp = nums.at(i) + nums.at(j) + nums.at(left) + nums.at(right);
if(temp == target){
result.push_back({nums.at(i),nums.at(j),nums.at(left),nums.at(right)});
left++;
right--;
])//避免重复
left++;
])
right--;
}else if(temp < target){
left++;
}else{
right--;
}
}
}
}
return result;
}
};
leetcode18—4Sum的更多相关文章
- [array] leetCode-18. 4Sum -Medium
18. 4Sum -Medium descrition Given an array S of n integers, are there elements a, b, c, and d in S s ...
- LeetCode18 4Sum
题意: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- ARTS第八周
1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有观点和思考的技术文章 以下是 ...
- LeetCode18: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 ...
- [Swift]LeetCode18. 四数之和 | 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [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:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum
18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c ...
随机推荐
- SQL Server 如何添加删除外键、主键,以及更新自增属性
1.添加删除主键和外键 例如: -----删除主键约束DECLARE @NAME SYSNAMEDECLARE @TB_NAME SYSNAMESET @TB_NAME = 'Date'SELECT ...
- 本地服务器搭建服务:ftp
开启FTP 服务针对局域网上需要管理的一些文件共享还是有一些帮助的,感兴趣的小伙伴可以尝试下: 1.开启internt 中ftp协议服务 完成即可 -> 可以访问了. tip: UTF-8 选f ...
- ThinkPHP5下自己写日志
1.首先在common.php公共函数文件下写需要的公共函数(appalication/common.php文件下),在此文件下写的函数可以在项目任意页面直接调用 /** * 打印log日志 * @p ...
- 关于ArrayAdapter的getCount()的方法会造成空指针异常的分析
在继承了ArrayAdapter重新getCount()的方法后,比如 public int getCount() { return 3; } 若在活动中调用setAdapter()的方法前,List ...
- java EE 监听器
生命周期监听器与属性改变监听器都必须使用@WebListener或在web.xml中声明,容器才会知道要加载.读取相关的监听器.
- ReactNative仿微信朋友圈App
摘要: 欢迎各位同学加入: React-Native群:397885169 大前端群:544587175 大神超多,热情无私帮助解决各种问题. 一.前沿||潜心修心,学无止尽.生活如此,coding亦 ...
- 【LLVM笔记】0x00 初识LLVM 链接类型
模块结构 LLVM程序是由若干的模块(Module)组成,每个模块中包含有一些函数.全局变量和符号表. 这些模块可能由LLVM的连接器组合在一起,组合的过程将会整合这些函数和全局变量的定义,整合他们的 ...
- go语言练习:类型转换
package main import "fmt" func main() { var a int var b uint var c float32 var d float64 a ...
- LeetCode题解之Maximum Depth of N-ary Tree
1.题目描述 2.问题分析 利用递归fangf 3.代码 int maxDepth(Node* root) { int res = maxdep(root); return res; } int ma ...
- MySQL: Connection Character Sets and Collations
character_set_server collation_servercharacter_set_databasecollation_database character_set_clientch ...