[leetcode]Permutations @ Python
2014-05-29 12:17
260 查看
原题地址:https://oj.leetcode.com/problems/permutations/
题意:
Given a collection of numbers, return all possible permutations.
For example,
解题思路:穷举一个集合的全排列。这个就是python递归巧妙的使用了。
代码:
题意:
Given a collection of 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], and
[3,2,1].
解题思路:穷举一个集合的全排列。这个就是python递归巧妙的使用了。
代码:
class Solution: # @param num, a list of integer # @return a list of lists of integers def permute(self, num): if len(num) == 0: return [] if len(num) == 1: return [num] res = [] for i in range(len(num)): for j in self.permute(num[:i] + num[i+1:]): res.append([num[i]] + j) return res
相关文章推荐
- [LeetCode]Permutations II@python
- 【Leetcode】【python】Permutations
- [leetcode]Permutations II @ Python
- [LeetCode]题解(python):046-Permutations
- 【LeetCode with Python】 Permutations
- 【LeetCode with Python】 Maximum Subarray
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- 【LeetCode with Python】 Reverse Words in a String
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Merge Two Sorted Lists
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Maximum Depth of Binary Tree
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 Pascal's Triangle