Share 4 sum leetcode c++,

angryzebra819

New member
#4 Sum, #LeetCode, #C ++, #Array, #AlGorithM ## 4 Sum trong LeetCode với C ++

Đưa ra một loạt các số nguyên `nums` và số nguyên` target`, hãy tìm tất cả bốn số nguyên ** ** trong `nums` tổng hợp lên đến` target`.

**Ví dụ 1:**

`` `
Đầu vào: nums = [1, 0, -1, 0, -2, 2], target = 0
Đầu ra: [[-2, -1, 1, 2], [-2, 0, 0, 2]]]
`` `

** Ví dụ 2: **

`` `
Đầu vào: nums = [-1, 0], target = -1
Đầu ra: [[-1, 0, 0]]
`` `

**Giải pháp:**

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// Độ phức tạp về thời gian: O (n^3)
// Độ phức tạp không gian: O (1)
Vector <Vector <int >> Foursum (Vector <Int> & Nums, int Target) {
Vector <Vector <int >> res;
int n = nums.size ();

// Sắp xếp mảng
sắp xếp (nums.begin (), nums.end ());

// lặp lại tất cả các cặp có thể
for (int i = 0; i <n - 3; i ++) {
for (int j = i+1; j <n - 2; j ++) {
// Tìm hai phần tử còn lại tổng hợp cho mục tiêu - (nums + nums [j])
int trái = j + 1;
int right = n - 1;
while (trái <phải) {
int sum = nums + nums [j] + nums [trái] + nums [phải];
if (sum == Target) {
res.push_back ({nums , nums [j], nums [trái], nums [phải]});
còn lại ++;
Phải--;
} if if (sum <target) {
còn lại ++;
} khác {
Phải--;
}
}
}
}

trả lại res;
}

int main () {
vector <int> nums = {1, 0, -1, 0, -2, 2};
int target = 0;

vector <vector <int >> res = foursum (nums, target);

for (vector <int> v: res) {
for (int i: v) {
cout << i << "";
}
cout << endl;
}

trả lại 0;
}
`` `

## hashtags

* #4sum
* #LeetCode
* #C ++
* #Mảng
* #AlGorithM
=======================================
#4 Sum, #LeetCode, #C++, #Array, #AlGorithM ## 4 Sum in Leetcode with C++

Given an array of integers `nums` and an integer `target`, find all four **unique** integers in `nums` that sum up to `target`.

**Example 1:**

```
Input: nums = [1, 0, -1, 0, -2, 2], target = 0
Output: [[-2, -1, 1, 2], [-2, 0, 0, 2]]
```

**Example 2:**

```
Input: nums = [-1, 0], target = -1
Output: [[-1, 0, 0]]
```

**Solution:**

```c++
#include <iostream>
#include <vector>

using namespace std;

// Time complexity: O(n^3)
// Space complexity: O(1)
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
int n = nums.size();

// Sort the array
sort(nums.begin(), nums.end());

// Iterate over all possible pairs
for (int i = 0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
// Find the two remaining elements that sum up to target - (nums + nums[j])
int left = j + 1;
int right = n - 1;
while (left < right) {
int sum = nums + nums[j] + nums
+ nums
;
if (sum == target) {
res.push_back({nums, nums[j], nums
, nums
});
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
}

return res;
}

int main() {
vector<int> nums = {1, 0, -1, 0, -2, 2};
int target = 0;

vector<vector<int>> res = fourSum(nums, target);

for (vector<int> v : res) {
for (int i : v) {
cout << i << " ";
}
cout << endl;
}

return 0;
}
```

## Hashtags

* #4sum
* #LeetCode
* #C++
* #Array
* #AlGorithM
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top