ngotuongbarron
New member
## Giải pháp 4Sum LeetCode trong C ++
Đưa ra một loạt các số nguyên `nums` và số nguyên` target`, tìm tất cả bốn phần tử ** subarrays ** của `nums` sao cho tổng của bốn phần tử bằng` target`.
A ** Subarray ** là một phần tiếp giáp của một mảng.
**Ví dụ 1:**
`` `
Đầu vào: nums = [1,0, -1,0, -2,2], Target = 0
Đầu ra: [[-1,0,0,1], [-2, -1,1,2]]]]
`` `
** Ví dụ 2: **
`` `
Đầu vào: nums = [-1,2,1, -4], Target = 1
Đầu ra: []
`` `
**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 trên tất cả các vòng đua 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ử khác tổng hợp cho mục tiêu - (nums + nums [j])
int k = j + 1;
int l = n - 1;
while (k <l) {
int sum = nums + nums [j] + nums [k] + nums [l];
if (sum == Target) {
res.push_back ({nums , nums [j], nums [k], nums [l]});
K ++;
l--;
} if if (sum <target) {
K ++;
} khác {
l--;
}
}
}
}
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> Quadplet: res) {
for (int num: tứ giác) {
cout << num << "";
}
cout << endl;
}
trả lại 0;
}
`` `
## hashtags
* #4sum
* #LeetCode
* #C ++
* #mảng
* #lập trình năng động
=======================================
## 4Sum Leetcode Solution in C++
Given an array of integers `nums` and an integer `target`, find all four-element **subarrays** of `nums` such that the sum of the four elements is equal to `target`.
A **subarray** is a contiguous part of an array.
**Example 1:**
```
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-1,0,0,1],[-2,-1,1,2]]
```
**Example 2:**
```
Input: nums = [-1,2,1,-4], target = 1
Output: []
```
**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 quadruplets
for (int i = 0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
// Find the two other elements that sum to target - (nums + nums[j])
int k = j + 1;
int l = n - 1;
while (k < l) {
int sum = nums + nums[j] + nums[k] + nums[l];
if (sum == target) {
res.push_back({nums, nums[j], nums[k], nums[l]});
k++;
l--;
} else if (sum < target) {
k++;
} else {
l--;
}
}
}
}
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> quadruplet : res) {
for (int num : quadruplet) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
```
## Hashtags
* #4sum
* #LeetCode
* #C++
* #Array
* #Dynamic programming
Đưa ra một loạt các số nguyên `nums` và số nguyên` target`, tìm tất cả bốn phần tử ** subarrays ** của `nums` sao cho tổng của bốn phần tử bằng` target`.
A ** Subarray ** là một phần tiếp giáp của một mảng.
**Ví dụ 1:**
`` `
Đầu vào: nums = [1,0, -1,0, -2,2], Target = 0
Đầu ra: [[-1,0,0,1], [-2, -1,1,2]]]]
`` `
** Ví dụ 2: **
`` `
Đầu vào: nums = [-1,2,1, -4], Target = 1
Đầu ra: []
`` `
**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 trên tất cả các vòng đua 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ử khác tổng hợp cho mục tiêu - (nums + nums [j])
int k = j + 1;
int l = n - 1;
while (k <l) {
int sum = nums + nums [j] + nums [k] + nums [l];
if (sum == Target) {
res.push_back ({nums , nums [j], nums [k], nums [l]});
K ++;
l--;
} if if (sum <target) {
K ++;
} khác {
l--;
}
}
}
}
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> Quadplet: res) {
for (int num: tứ giác) {
cout << num << "";
}
cout << endl;
}
trả lại 0;
}
`` `
## hashtags
* #4sum
* #LeetCode
* #C ++
* #mảng
* #lập trình năng động
=======================================
## 4Sum Leetcode Solution in C++
Given an array of integers `nums` and an integer `target`, find all four-element **subarrays** of `nums` such that the sum of the four elements is equal to `target`.
A **subarray** is a contiguous part of an array.
**Example 1:**
```
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-1,0,0,1],[-2,-1,1,2]]
```
**Example 2:**
```
Input: nums = [-1,2,1,-4], target = 1
Output: []
```
**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 quadruplets
for (int i = 0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
// Find the two other elements that sum to target - (nums + nums[j])
int k = j + 1;
int l = n - 1;
while (k < l) {
int sum = nums + nums[j] + nums[k] + nums[l];
if (sum == target) {
res.push_back({nums, nums[j], nums[k], nums[l]});
k++;
l--;
} else if (sum < target) {
k++;
} else {
l--;
}
}
}
}
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> quadruplet : res) {
for (int num : quadruplet) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
```
## Hashtags
* #4sum
* #LeetCode
* #C++
* #Array
* #Dynamic programming