美文网首页
46. Permutations

46. Permutations

作者: jecyhw | 来源:发表于2019-05-27 06:13 被阅读0次

题目链接

https://leetcode.com/problems/permutations/

解题思路

dfs

代码

class Solution {
public:
    void dfs(vector<vector<int>> &ans, vector<int> &nums, int ind) {
        if (ind >= nums.size()) {
            ans.push_back(nums);
        }

        for (int i = ind; i < nums.size(); ++i) {
            swap(nums[ind], nums[i]);
            dfs(ans, nums, ind + 1);
            swap(nums[ind], nums[i]);
        }
    }

    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> ans;
        dfs(ans, nums, 0);
        return ans;
    }
};

相关文章

网友评论

      本文标题:46. Permutations

      本文链接:https://www.haomeiwen.com/subject/kcoqtctx.html