算法分析与设计课程(3):【leetcode】Permutations
来源:互联网 发布:游戏大厅源码 编辑:程序博客网 时间:2024/04/29 05:30
Permutations
Description:
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:算法分析:解决这个问题,很容易想到树,一层层地生成。
因此我们可以考虑用递归的方式。
代码如下:
__author__ = 'andy'class Solution(object): def permute(self, nums): self.result = [] subList = [] self.cal(nums,subList) return self.result def cal(self,nums,subList): #如果子链表跟原链表等长,则加到结果中 if len(nums)==len(subList): self.result.append(subList[:]) #用递归的方式,得出组合 for a in nums: if a in subList: continue subList.append(a) self.cal(nums,subList) subList.remove(a)case = Solution()nums =[1,2,3]print(case.permute(nums))
结果:
0 0
- 算法分析与设计课程(3):【leetcode】Permutations
- 算法分析与设计课程(4):【leetcode】Wildcard Matching
- 算法分析与设计课程(8):【leetcode】Sudoku Solver
- 算法分析与设计课程(9):【leetcode】Jump Game
- 算法分析与设计课程(10):【leetcode】Simplify Path
- 算法分析与设计课程(11):【leetcode】Gray Code
- 算法分析与设计课程(14):【leetcode】 Maximal Square
- 算法分析与设计课程(17):【leetcode】4Sum
- 算法分析与设计课程(18):【leetcode】Clone Graph
- 算法分析与设计课程(5):【leetcode】Search for a Range
- 算法分析与设计课程(7):【leetcode】Reverse Nodes in k-Group
- 算法分析与设计课程(12):【leetcode】 Count Complete Tree Nodes
- 算法分析与设计课程(13):【leetcode】 Product of Array Except Self
- 算法分析与设计课程(15):【leetcode】 Find Minimum in Rotated Sorted Array
- 算法分析与设计课程(16):【leetcode】 Integer to Roman
- 算法分析与设计课程(19):【leetcode】Ugly Number II
- 设计Huffman编码(算法设计与分析课程实验)
- 算法设计与分析课程Part1笔记(3)
- Vue.js computed vs Methods
- iOS:圆形头像和菱形头像的实现
- 计算机图形学(四)_几何变换_1_基本的二维几何变换(三)_缩放
- android surfaceflinger研究----Surface机制
- jQueryDOM操作
- 算法分析与设计课程(3):【leetcode】Permutations
- git服务器搭建
- String转换Long两种方式
- NAPI机制分析
- java HashMap的实现
- DispatcherServlet详解 —SpringMVC
- css中巧用padding
- 自定义ndk-build后lib的生成目录
- 折线图的制作(java)