LeetCode - Permutations

本文介绍了如何通过递归和动态规划的方法,利用一个向量来存储哪些数字已经被访问过,从而高效地生成一组数字的所有可能排列组合。详细解释了算法的实现过程,并提供了一个具体的C++代码示例,该代码可在LeetCode平台上的相关问题中应用。

Given a collection of numbers, return all possible permutations.

For example,

[1,2,3] have the following permutations:

[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

http://oj.leetcode.com/problems/permutations/


Solution:

Recursion and dp, use a vector to store which digit has been visited.

https://github.com/starcroce/leetcode/blob/master/permuntations.cpp

// 68 ms for 25 test cases
// use a visited vector to store which digit has been visited
class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<vector<int> > res;
        if(num.size() == 0) {
            return res;
        }
        vector<int> sol;
        vector<bool> visited(num.size(), false);
        permute(num, 0, visited, res, sol);
        return res;
    }
    
    void permute(vector<int> &num, int level, vector<bool> visited, vector<vector<int> > &res, vector<int> sol) {
        if(sol.size() == num.size()) {
            res.push_back(sol);
            return;
        }
        for(int i = 0; i < num.size(); i++) {
            if(visited[i] == false) {
                visited[i] = true;
                sol.push_back(num[i]);
                permute(num, i+1, visited, res, sol);
                sol.pop_back();
                visited[i] = false;
            }
        }
    }
};


你提供的代码是 **LeetCode 第46题:全排列(Permutations)** 的官方题解,使用 **回溯 + 交换法** 实现所有元素的全排列。该算法通过递归地交换数组中的元素,生成所有可能的排列组合。 --- ## ✅ 代码解析与完整 VS2022 可运行版本 以下是适合在 **Visual Studio 2022 (VS2022)** 中运行的完整代码版本,包含 `vector`、主函数和测试用例。 ```cpp #include <iostream> #include <vector> using namespace std; class Solution { public: void backtrack(vector<vector<int>>& res, vector<int>& output, int first, int len) { // 所有数都填完了 if (first == len) { res.emplace_back(output); return; } for (int i = first; i < len; ++i) { // 动态维护数组:交换当前元素和第一个未固定元素 swap(output[i], output[first]); // 继续递归填下一个数 backtrack(res, output, first + 1, len); // 撤销交换,恢复现场 swap(output[i], output[first]); } } vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> res; backtrack(res, nums, 0, (int)nums.size()); return res; } }; // 打印结果的辅助函数 void printResult(const vector<vector<int>>& result) { cout << "[\n"; for (const auto& permutation : result) { cout << " ["; for (size_t i = 0; i < permutation.size(); ++i) { cout << permutation[i]; if (i != permutation.size() - 1) cout << ", "; } cout << "]\n"; } cout << "]\n"; } // 主函数测试 int main() { Solution sol; vector<int> nums; // 测试用例 1 nums = {1, 2, 3}; cout << "输入数组: "; for (int num : nums) cout << num << " "; cout << "\n所有全排列为:\n"; printResult(sol.permute(nums)); cout << endl; // 测试用例 2 nums = {0, 1}; cout << "输入数组: "; for (int num : nums) cout << num << " "; cout << "\n所有全排列为:\n"; printResult(sol.permute(nums)); cout << endl; // 测试用例 3 nums = {1}; cout << "输入数组: "; for (int num : nums) cout << num << " "; cout << "\n所有全排列为:\n"; printResult(sol.permute(nums)); cout << endl; return 0; } ``` --- ## ✅ 示例输出 ``` 输入数组: 1 2 3 所有全排列为: [ [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 2, 1] [3, 1, 2] ] 输入数组: 0 1 所有全排列为: [ [0, 1] [1, 0] ] 输入数组: 1 所有全排列为: [ [1] ] ``` --- ## ✅ 算法逻辑详解 ### ✅ 问题背景 给定一个不含重复数字的数组 `nums`,返回其所有可能的全排列。 ### ✅ 解法思路:回溯 + 交换法 #### 步骤: 1. **递归终止条件**: - `first == nums.size()`:所有元素都已确定位置,将当前排列加入结果集 2. **递归过程**: - 从 `first` 到 `nums.size() - 1` 遍历所有可能的起始元素 - 将当前元素 `nums[i]` 与 `nums[first]` 交换,表示固定当前元素到第一个位置 - 递归调用 `backtrack(..., first + 1)` 处理下一个位置 - 回溯:交换回来,恢复原数组状态,以便下一次尝试 #### 优点: - 不需要额外空间保存已使用元素,直接在原数组上操作 - 时间复杂度低,适合中等规模输入 --- ## ✅ 时间与空间复杂度 | 类型 | 复杂度 | 说明 | |------|--------|------| | 时间复杂度 | O(n × n!) | 共 `n!` 个排列,每个排列需要 O(n) 的时间生成 | | 空间复杂度 | O(n) | 递归栈深度为 `n` | --- ## ✅ 常见问题排查(VS2022) 1. **编译错误** - 确保包含 `<vector>` 和 `<iostream>` - 使用 `using namespace std;` 或加上 `std::` 前缀 2. **运行时错误** - 注意空数组处理 - 检查 `swap()` 是否越界 3. **逻辑错误** - 确保回溯后恢复数组状态 - 确保递归终止条件正确 --- ## ✅ 对比其他解法 | 解法 | 时间复杂度 | 空间复杂度 | 特点 | |------|------------|------------|------| | 回溯 + 交换法(当前方法) | O(n × n!) | O(n) | 无需额外空间,高效 | | 回溯 + 标记数组 | O(n × n!) | O(n) | 更直观,但需要额外数组 | | STL `next_permutation` | O(n × n!) | O(n) | 利用库函数,适合快速实现 | | 递归生成法(分治) | O(n × n!) | O(n) | 逻辑清晰,但代码略复杂 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值