Share 47. permutations ii c++,

#PerMutations, #C ++, #backtracking, #AlGorithM, #Interview ** 47.Hoán vị ii trong c ++ **

Trong bài viết này, chúng tôi sẽ thảo luận về cách tìm tất cả các hoán vị của một tập hợp số nhất định trong C ++.Chúng tôi sẽ sử dụng thuật toán quay lại để giải quyết vấn đề này.

** Thuật toán quay lại **

Thuật toán quay lại là một thuật toán đệ quy có thể được sử dụng để tìm tất cả các giải pháp có thể cho một vấn đề nhất định.Thuật toán hoạt động bằng cách bắt đầu với một giải pháp một phần và sau đó cố gắng mở rộng nó sang một giải pháp hoàn chỉnh.Nếu phần mở rộng là không thể, thuật toán quay lại và thử một phần mở rộng khác.

Thuật toán quay lại để tìm tất cả các hoán vị của một tập hợp các số hoạt động như sau:

1. Bắt đầu với bộ trống làm giải pháp một phần.
2. Đối với mỗi số trong tập hợp, hãy thêm nó vào giải pháp một phần và gọi lại thuật toán quay lại trên các số còn lại.
3. Nếu thuật toán quay lại trả về một giải pháp hoàn chỉnh, hãy thêm nó vào danh sách tất cả các giải pháp.
4. Backtrack và loại bỏ số cuối cùng khỏi giải pháp một phần.

** Việc triển khai C ++ **

Sau đây là việc triển khai C ++ của thuật toán quay lại để tìm tất cả các hoán vị của một tập hợp số:

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

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

// một chức năng để in tất cả các hoán vị của một bộ số nhất định
void printPermutations (vector <tint> nums) {
// Tạo một vectơ để lưu trữ tất cả các hoán vị
Vector <Vector <int >> hoán vị;

// tìm kiếm tất cả các hoán vị của bộ số đã cho
findpermutations (nums, 0, hoán vị);

// in tất cả các hoán vị
for (int i = 0; i <perputations.size (); i ++) {
for (int j = 0; j <permutations .size (); j ++) {
cout << hoán vị [j] << "";
}
cout << endl;
}
}

// một hàm đệ quy để tìm tất cả các hoán vị của một bộ số nhất định
void findPermutations (vector <int> nums, int start, vector <vector <int >> & permutations) {
// Nếu chỉ số bắt đầu bằng kích thước của tập hợp, thì chúng tôi đã tìm thấy hoán vị
if (start == nums.size ()) {
hoán vị.push_back (nums);
trở lại;
}

// Đối với mỗi số trong tập hợp, hãy thêm nó vào giải pháp một phần và gọi lại chức năng FindPermutation
for (int i = start; i <nums.size (); i ++) {
// hoán đổi số hiện tại với số ở chỉ mục bắt đầu
int temp = nums ;
nums = nums [start];
nums [start] = temp;

// tìm kiếm tất cả các hoán vị của các số còn lại
FindPermutations (nums, start + 1, hoán vị);

// hoán đổi số hiện tại trở lại với số ở chỉ mục bắt đầu
temp = nums ;
nums = nums [start];
nums [start] = temp;
}
}

int main () {
// Tạo một vectơ số
vector <Int> nums = {1, 2, 3};

// in tất cả các hoán vị của bộ số đã cho
printPermutations (nums);

trả lại 0;
}
`` `

** Đầu ra **

`` `
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
`` `

** hashtags **

#PerMutations
#C ++
#backtracking
#AlGorithM
#Phỏng vấn
=======================================
#PerMutations, #C++, #backtracking, #AlGorithM, #Interview **47. Permutations II in C++**

In this article, we will discuss how to find all permutations of a given set of numbers in C++. We will use the backtracking algorithm to solve this problem.

**The Backtracking Algorithm**

The backtracking algorithm is a recursive algorithm that can be used to find all possible solutions to a given problem. The algorithm works by starting with a partial solution and then trying to extend it to a complete solution. If the extension is not possible, the algorithm backtracks and tries a different extension.

The backtracking algorithm for finding all permutations of a set of numbers works as follows:

1. Start with the empty set as the partial solution.
2. For each number in the set, add it to the partial solution and recursively call the backtracking algorithm on the remaining numbers.
3. If the backtracking algorithm returns a complete solution, add it to the list of all solutions.
4. Backtrack and remove the last number from the partial solution.

**The C++ Implementation**

The following is the C++ implementation of the backtracking algorithm for finding all permutations of a set of numbers:

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

using namespace std;

// A function to print all permutations of a given set of numbers
void printPermutations(vector<int> nums) {
// Create a vector to store all permutations
vector<vector<int>> permutations;

// Recursively find all permutations of the given set of numbers
findPermutations(nums, 0, permutations);

// Print all permutations
for (int i = 0; i < permutations.size(); i++) {
for (int j = 0; j < permutations.size(); j++) {
cout << permutations[j] << " ";
}
cout << endl;
}
}

// A recursive function to find all permutations of a given set of numbers
void findPermutations(vector<int> nums, int start, vector<vector<int>> &permutations) {
// If the start index is equal to the size of the set, then we have found a permutation
if (start == nums.size()) {
permutations.push_back(nums);
return;
}

// For each number in the set, add it to the partial solution and recursively call the findPermutations function
for (int i = start; i < nums.size(); i++) {
// Swap the current number with the number at the start index
int temp = nums;
nums = nums[start];
nums[start] = temp;

// Recursively find all permutations of the remaining numbers
findPermutations(nums, start + 1, permutations);

// Swap the current number back with the number at the start index
temp = nums;
nums = nums[start];
nums[start] = temp;
}
}

int main() {
// Create a vector of numbers
vector<int> nums = {1, 2, 3};

// Print all permutations of the given set of numbers
printPermutations(nums);

return 0;
}
```

**Output**

```
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
```

**Hashtags**

#PerMutations
#C++
#backtracking
#AlGorithM
#Interview
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top