zoukankan      html  css  js  c++  java
  • 46. Permutations

    Given a collection of distinct 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],
      [3,2,1]
    ]

    题解

    该题是求所有可能的排列组合,是一道典型的dfs类型的题

    代码(python实现)

    import copy
    class Solution(object):
        def permute(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if nums is None:
                return
            results = []
            if len(nums) == 0:
                return results
            self.helper(nums, results, [])
            return results
        def helper(self, nums, results, list):
            
            if len(list) == len(nums):
                #注意此处是深度拷贝,如果用java语言写则以list为输入new出一个新的实例
                results.append(copy.deepcopy(list))
                return
                
            
            for i in range(len(nums)):
                if(nums[i] in list):
                    continue
                
                list.append(nums[i])
                self.helper(nums, results, list)
                list.pop()
  • 相关阅读:
    事件类型
    program的发展史与两个数学方法
    字符统计与基本正则
    bom与dom
    css长度单位及颜色表示
    grid学习
    position定位的认识
    remark:node端口的close-wait
    css background属性设置
    Promise之我发现
  • 原文地址:https://www.cnblogs.com/bubbleStar/p/6886659.html
Copyright © 2011-2022 走看看