zoukankan      html  css  js  c++  java
  • leetcode_1:permute

    class Solution:
        def permute(self, nums: List[int]) -> List[List[int]]:
            res = []
            def helper(nums, temp):
                if not nums:
                    if temp not in res:
                        res.append(temp)
                for i in range(len(nums)):
                    helper(nums[:i] + nums[i + 1:], temp + [nums[i]])
    
            helper(nums, [])
            return res
    
    class Solution:
        def permute(self, nums: List[int]) -> List[List[int]]:
            def helper(path):
                if len(path) == l:
                    res.append(path[:])
                for i in range(l):
                    if not used[i]:
                        used[i] = True
                        path.append(nums[i])
                        helper(path)
                        path.pop(-1)
                        used[i] = False
    
            l = len(nums)
            res = []
            used = [False for _ in nums]
            helper([])
            return res
    
    class Solution {
    public:
        void helper(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[first], output[i]);
                helper(res, output, first + 1, len);
                swap(output[first], output[i]);
            }
    
        }
        vector<vector<int>> permute(vector<int>& nums) {
            vector<vector<int>> res;
            helper(res, nums, 0, (int)nums.size());
            return res;
        }
    };
    
  • 相关阅读:
    [LeetCode] Wiggle Sort
    [LeetCode] Perfect Squares
    [LeetCode] Minimum Window Substring
    [LeetCode] Valid Sudoku
    [LeetCode] Sudoku Solver
    [LeetCode] First Bad Version
    [LeetCode] Find the Celebrity
    [LeetCode] Paint Fence
    [LeetCode] H-Index II
    [LeetCode] H-Index
  • 原文地址:https://www.cnblogs.com/fengcnblogs/p/13788681.html
Copyright © 2011-2022 走看看