LeetCode – Refresh – 4sum

To be honest, I dont like this problem. This is just an extra layer loop for 3sum. But two mistakes I had made…..

1. when skip j, use condition j > i+1. Because it starts from i+1. not from i.

2. Typo or my brain is gone… condition for l is num[l] == num[l+1] since l is decreasing.

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

LeetCode – Refresh – 4sum的相关文章

LeetCode 017 4Sum

[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending o

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&

[LeetCode] 018. 4Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 018.4Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/4sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给一个数列 S ,找出四个数 a,b,c,d 使得a + b + c + d = targ

[LeetCode] 454. 4Sum II 四数之和II

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the r

[LeetCode] 18. 4Sum ☆☆

Given an array S of n integers, are there elements a, b, c, and d in S 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. For exampl

【Leetcode】4Sum

题目链接:https://leetcode.com/problems/4sum/ 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadrupl

[LeetCode][JavaScript]4Sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending o

LeetCode——18. 4Sum

一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的. 三.题解: 这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或