首页 > 代码库 > LeetCode——Permutations
LeetCode——Permutations
Permutations
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 Permutations: def perm(self, num, li, start, end): s = [] if start == end: for i in num: s.append(i) li.append(s) else: for i in range(start, end): num[start], num[i] = num[i], num[start] self.perm(num, li, start + 1, end) num[start], num[i] = num[i], num[start] # @param num, a list of integer # @return a list of lists of integers def permute(self, num): li = [] if len(num) == 0: return [] if len(num) == 1: return [num] self.perm(num, li, 0, len(num)) return lipe = Permutations()pe.permute([3, 2, 1])
LeetCode——Permutations
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。