15.3sum leetcode(array)
来源:互联网 发布:win7组策略设置优化 编辑:程序博客网 时间:2024/05/01 06:28
3Sum Add to List QuestionEditorial Solution
Given an array S of n integers, are there elements a, b, c in S 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.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int> > ret; int size = num.size(); sort(num.begin(), num.end()); for(int i = 0; i < size; i ++) { //skip same i while(i > 0 && i < size && num[i] == num[i-1]) i ++; int j = i + 1; int k = size - 1; while(j < k) { int sum = num[i] + num[j] + num[k]; if(sum == 0) { vector<int> cur(3); cur[0] = num[i]; cur[1] = num[j]; cur[2] = num[k]; ret.push_back(cur); j ++; k --; //skip same j while(j < k && num[j] == num[j-1]) j ++; //skip same k while(k > j && num[k] == num[k+1]) k --; } else if(sum < 0) { j ++; } else { k --; } } } return ret; }
0 0
- 15.3sum leetcode(array)
- LeetCode[Array]----3Sum
- LeetCode[Array]---- 4Sum
- LeetCode--Array---4Sum
- Leetcode - Pointers/Array - Two sum
- LeetCode[Array]----3Sum Closest
- LeetCode: Split Array Largest Sum
- 1.Two Sum leetcode(array)
- LeetCode-Array-1. Two Sum
- [Leetcode] Split Array Largest Sum
- LeetCode刷题【Array】 Two Sum
- leetcode.array--1. Two Sum
- Array -- Leetcode problem1. Two Sum
- leetcode.array--15. 3Sum
- leetcode.array--18. 4Sum
- leetcode.array--39. Combination Sum
- LeetCode(Array)---------Two Sum
- 【Leetcode】410. Split Array Largest Sum
- animation 动画
- Qt中对图片中的颜色进行修改
- vim学习
- Dockerfile 最佳实践
- HBase1.2.3安装与配置------Hadoop学习(3)
- 15.3sum leetcode(array)
- 简单高效的nodejs爬虫模型
- Hibernate的参数绑定
- 输出年月日
- 多线程与timer
- unity安装与破解
- java泛型
- Google Testing on the Toilet系列---好的端对端测试是什么
- postgresql 安装教程