LeetCode – Refresh – 3sum

For this problem, do not forget to skip the duplicates for i. Usually I will do duplication removal for j and k.

Tag it.

 1 class Solution {
 2 public:
 3     vector<vector<int> > threeSum(vector<int> &num) {
 4         vector<vector<int> > result;
 5         int len = num.size(), i = 0, j = 0, k = 0;
 6         if (len < 2) return result;
 7         sort(num.begin(), num.end());
 8         for (int i = 0; i < len-2; i++) {
 9             if (i > 0 && num[i] == num[i-1]) continue;
10             j = i+1, k = len-1;
11             while (j < k) {
12                 int sum = num[i] + num[j] + num[k];
13                 if (sum > 0) k--;
14                 else if (sum < 0) j++;
15                 else {
16                     vector<int> tmp;
17                     tmp.push_back(num[i]);
18                     tmp.push_back(num[j]);
19                     tmp.push_back(num[k]);
20                     result.push_back(tmp);
21                     do{j++;} while (j < k && num[j] == num[j-1]);
22                     do{k--;} while (j < k && num[k] == num[k+1]);
23                 }
24             }
25         }
26         return result;
27     }
28 };
时间: 2024-10-27 19:59:51

LeetCode – Refresh – 3sum的相关文章

LeetCode 015 3Sum

[题目] 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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

LeetCode 016 3Sum Closest

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int

[LeetCode] 015. 3Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 015.3Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和为 0. 分析: 先排序,再左右夹逼,复杂度 O(n*n).

LeetCode - Refresh - Pascal&#39;s Triangle II

Exact same as I. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if (rowIndex < 0) return vector<int> (); 5 vector<int> result(1, 1); 6 for (int i = 1; i <= rowIndex; i++) { 7 for (int j = result.size()-1; j >

LeetCode - Refresh - Pascal&#39;s Triangle

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end. 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 vector<vector&

[C++]LeetCode: 70 3Sum

题目: 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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

[LeetCode][JavaScript]3Sum Closest

3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given arr

[Leetcode][015] 3Sum (Java)

题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过了很久,第二次做,还是不会…….好几次都是Time Limited Error.在看过正确答案之后,才知道是用的Two Pointers + sort 做的优化. 怎么优化? 简单说,就是通过 排序 + 跳过重复(利用Two Pointers) 来达到题目中避免 duplicates的要求. 核心思