Day 11 of Algorithm Testing: Recursion/Backtracking--2

 

Table of contents

1. Combination

See the solution: 

Two, full arrangement

See the solution: 

3. Full arrangement of letter sizes

1. Combination

77. Combinations - LeetCode https://leetcode.cn/problems/combinations/?plan=algorithms&plan_progress=gzwnnxs

See the solution: 

Combination - Combination - LeetCode https://leetcode.cn/problems/combinations/solution/zu-he-by-leetcode-solution/

Two, full arrangement

46. ​​Full permutation - LeetCode https://leetcode.cn/problems/permutations/?plan=algorithms&plan_progress=gzwnnxs

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;
    }
};

See the solution: 

Full arrangement - Full arrangement - LeetCode https://leetcode.cn/problems/permutations/solution/quan-pai-lie-by-leetcode-solution-2/

3. Full arrangement of letter sizes

 Arrange letters in all upper and lower cases - Arrange letters in all upper and lower cases - LeetCode https://leetcode.cn/problems/letter-case-permutation/solution/zi-mu-da-xiao-xie-quan-pai-lie -by-leetcode/

Guess you like

Origin blog.csdn.net/m0_63309778/article/details/126755113