3Sum(or k_Sum)
Given an array nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
(4Sum问题)
C++:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
if (nums.size() <= )return result;
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size() - ; i++) {
int a = nums[i];
if (a > ) break;
if (i > && a == nums[i - ]) continue;
for (long j = i + , k = nums.size() - ; j < k;) {
int b = nums[j];
int c = nums[k];
int value = a + b + c;
if (value == ) {
result.push_back(vector<int>({ a, b, c }));
while (b == nums[++j] && j<k);
while (c == nums[--k] && j<k);
}
else if (value > ) {
k--;
}
else {
j++;
}
}
}
return result;
}
};
3Sum(or k_Sum)的更多相关文章
- LeetCode: 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- Leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 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 ...
- 16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- fedora、centos、rhel安装Adobe Flash Player 28
切换到root用户 添加Adobe Repository Adobe Repository 32-bit x86 rpm -ivh http://linuxdownload.adobe.com/ado ...
- 团队作业——Beta冲刺4
团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:在同队成员帮助下,完成了浏览详情界面的跳转,以及图片的嵌入 明日任务:继续完成浏览详情界面 吴松青 今日任务:研究图片详情界面后端函数,遇到 ...
- spark的shuffle和原理分析
概述 Shuffle就是对数据进行重组,由于分布式计算的特性和要求,在实现细节上更加繁琐和复杂. 在MapReduce框架,Shuffle是连接Map和Reduce之间的桥梁,Map阶段 ...
- sublime text3 setting-user
{ "caret_style": "smooth", "find_selected_text": true, "font_size ...
- python第二十九课——文件读写(复制文件)
自定义函数:实现文件复制操作有形参(2个) 没有返回值相似版(不用) def copyFile(src,dest): #1.打开两个文件:1个关联读操作,1个关联写操作 fr=open(src,'rb ...
- 【洛谷P3410】拍照题解(最大权闭合子图总结)
题目描述 小B有n个下属,现小B要带着一些下属让别人拍照. 有m个人,每个人都愿意付给小B一定钱让n个人中的一些人进行合影.如果这一些人没带齐那么就不能拍照,小B也不会得到钱. 注意:带下属不是白带的 ...
- [题目] Luogu P3716 [CTSC2000]冰原探险
题面 题目背景 传说中,南极有一片广阔的冰原,在冰原下藏有史前文明的遗址.整个冰原被横竖划分成了很多个大小相等的方格.在这个冰原上有N个大小不等的矩形冰山,这些巨大的冰山有着和南极一样古老的历史,每个 ...
- php无限分类 下拉框
无限分类 下拉框优势:填写参数少,只需要指定一个循环节点($parnent_id),就可以循环所有下级分类.循环输出结构很有特色,比较符合我的口味.补充: $parent_id才是上下级关联的节点,i ...
- std::lexicographical_compare函数的使用
按照词典序比较前者是否小于后者. 当序列<first1, last1>按照字典序比较小于后者序列<first2, last2>,则返回true.否则,返回false. 所谓字典 ...
- 求 1+2+3+ …… +n
题目来源: 自我感觉难度/真实难度: 题意: 分析: 自己的代码: def Sum_Solution(n): # write code here if n==1: return 1 else: ans ...