Share 3sum problem leetcode c++,

#3SUM #LeetCode #C ++ #Programming #algorithms ## Bài toán 3SUM trong LeetCode với C ++

Vấn đề 3Sum là một vấn đề kinh điển trong khoa học máy tính.Đưa ra một loạt các số nguyên, tìm tất cả các bộ ba phần tử có tổng là bằng không.

Vấn đề này có thể được giải quyết trong thời gian O (n^2) bằng cách sử dụng phương pháp tiếp cận vũ phu.Tuy nhiên, có các thuật toán hiệu quả hơn có thể được sử dụng để giải quyết vấn đề này trong thời gian O (n log n).

Trong bài viết này, chúng tôi sẽ thảo luận về cách giải quyết vấn đề 3SUM trong C ++ bằng cách sử dụng phương pháp hai con trỏ.Cách tiếp cận này là một sự thay thế hiệu quả hơn cho phương pháp vũ phu và có thể được sử dụng để giải quyết vấn đề trong thời gian O (n log n).

### Cách tiếp cận hai con trỏ

Cách tiếp cận hai con trỏ hoạt động bằng cách đầu tiên sắp xếp mảng đầu vào.Điều này cho phép chúng tôi so sánh các yếu tố trong mảng một cách hiệu quả hơn.

Khi mảng được sắp xếp, chúng ta có thể sử dụng hai con trỏ để lặp qua mảng.Con trỏ đầu tiên, tôi, sẽ bắt đầu khi bắt đầu mảng.Con trỏ thứ hai, J, sẽ bắt đầu ở cuối mảng.

Sau đó, chúng tôi sẽ lặp lại thông qua mảng, bắt đầu bằng i = 0 và j = n - 1. Ở mỗi lần lặp, chúng tôi sẽ kiểm tra xem tổng các phần tử tại các chỉ số I và J bằng không.Nếu có, chúng tôi sẽ tìm ra giải pháp cho vấn đề 3SUM.

Nếu tổng của các phần tử tại các chỉ số I và J không bằng 0, chúng ta sẽ di chuyển một trong các con trỏ (I hoặc J) theo hướng sẽ làm cho tổng gần hơn với 0.

Chúng tôi sẽ tiếp tục lặp lại thông qua mảng cho đến khi chúng tôi tìm thấy một giải pháp cho vấn đề 3SUM hoặc chúng tôi đi đến cuối mảng.

### Thực hiện

Sau đây là việc thực hiện phương pháp hai con trỏ trong C ++:

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

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

// Hàm này trả về đúng nếu tổng của các phần tử tại các chỉ số I, J và K bằng 0.
Bool Hassum (Vector <Int> & Nums, Int I, Int J, Int K) {
trả về nums + nums [j] + nums [k] == 0;
}

// Hàm này tìm thấy tất cả các bộ ba phần tử trong mảng đã cho có tổng bằng 0.
void findTriplets (vector <tint> & nums) {
// Sắp xếp mảng.
sắp xếp (nums.begin (), nums.end ());

// lặp qua mảng, bắt đầu bằng i = 0 và j = n - 1.
for (int i = 0; i <nums.size () - 2; i ++) {
// lặp qua mảng, bắt đầu bằng j = n - 1.
for (int j = nums.size ()-1; j> i; j--) {
// lặp qua mảng, bắt đầu bằng k = i + 1.
for (int k = i+1; k <j; k ++) {
// Kiểm tra xem tổng của các phần tử tại các chỉ số I, J và K bằng 0.
if (Hassum (nums, i, j, k)) {
// In bộ ba của các yếu tố.
cout << nums << "," << nums [j] << "," << nums [k] << endl;
}
}
}
}
}

int main () {
// Tạo một mảng số nguyên.
vector <int> nums = {-1, 0, 1, 2, -1, -4};

// Tìm tất cả các bộ ba phần tử trong mảng có tổng bằng 0.
findTriplets (nums);

trả lại 0;
}
`` `

### Chạy mã

Để chạy mã,
=======================================
#3SUM #LeetCode #C++ #Programming #algorithms ## 3Sum Problem in Leetcode with C++

The 3Sum problem is a classic problem in computer science. Given an array of integers, find all triplets of elements whose sum is zero.

This problem can be solved in O(n^2) time using a brute-force approach. However, there are more efficient algorithms that can be used to solve this problem in O(n log n) time.

In this article, we will discuss how to solve the 3Sum problem in C++ using the two-pointer approach. This approach is a more efficient alternative to the brute-force approach and can be used to solve the problem in O(n log n) time.

### The Two-Pointer Approach

The two-pointer approach works by first sorting the input array. This allows us to compare elements in the array in a more efficient way.

Once the array is sorted, we can use two pointers to iterate through the array. The first pointer, i, will start at the beginning of the array. The second pointer, j, will start at the end of the array.

We will then iterate through the array, starting with i = 0 and j = n - 1. At each iteration, we will check if the sum of the elements at indices i and j is equal to zero. If it is, we will have found a solution to the 3Sum problem.

If the sum of the elements at indices i and j is not equal to zero, we will move one of the pointers (either i or j) in the direction that will make the sum closer to zero.

We will continue iterating through the array until either we find a solution to the 3Sum problem or we reach the end of the array.

### Implementation

The following is an implementation of the two-pointer approach in C++:

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

using namespace std;

// This function returns true if the sum of the elements at indices i, j, and k is equal to 0.
bool hasSum(vector<int>& nums, int i, int j, int k) {
return nums + nums[j] + nums[k] == 0;
}

// This function finds all triplets of elements in the given array whose sum is equal to 0.
void findTriplets(vector<int>& nums) {
// Sort the array.
sort(nums.begin(), nums.end());

// Iterate through the array, starting with i = 0 and j = n - 1.
for (int i = 0; i < nums.size() - 2; i++) {
// Iterate through the array, starting with j = n - 1.
for (int j = nums.size() - 1; j > i; j--) {
// Iterate through the array, starting with k = i + 1.
for (int k = i + 1; k < j; k++) {
// Check if the sum of the elements at indices i, j, and k is equal to 0.
if (hasSum(nums, i, j, k)) {
// Print the triplet of elements.
cout << nums << ", " << nums[j] << ", " << nums[k] << endl;
}
}
}
}
}

int main() {
// Create an array of integers.
vector<int> nums = {-1, 0, 1, 2, -1, -4};

// Find all triplets of elements in the array whose sum is equal to 0.
findTriplets(nums);

return 0;
}
```

### Running the Code

To run the code,
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top