【Leetcode】【python】Permutations
来源:互联网 发布:去除淘宝店铺模块缝隙 编辑:程序博客网 时间:2024/06/05 03:59
题目大意
求一组数的全排列
解题思路
回溯,想起来思路很简单,实际写的时候遇到了很多麻烦。
代码
正常思路
class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [] self.get_permute([], nums, result) return result def get_permute(self, current, num, result): if not num: # 如果num里没有数了,加到结果集,结束函数 # print 'benlun:', current, current + [] result.append(current+[]) # 不加[],结果是混乱的,没搞懂 return for i in range(len(num)): current.append(num[i]) # print current, num[:i] + num[i + 1:], 'result', result self.get_permute(current, num[:i] + num[i + 1:], result) # num改为拿走nums[i]后的数列 current.pop()
稍微难理解
class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if len(nums) <= 1: return [nums] ans = [] for i, num in enumerate(nums): n = nums[:i] + nums[i+1:] # n是剩余数的list for y in self.permute(n): # 直到函数有return,一个数的时候[nums],所以y是list ans.append([num] + y) return ans
总结
阅读全文
0 0
- 【LeetCode with Python】 Permutations
- 46. Permutations Leetcode Python
- 【Leetcode】【python】Permutations
- 【Leetcode】【python】Permutations II
- 【LeetCode with Python】 Permutations II
- 【leetcode】 46. Permutations ,python实现
- [leetcode]47. Permutations II,python实现
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- 如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境
- c++位运算
- MySQL日期的加减
- swift学习日记
- MySQL命令-- mysqlbinlog
- 【Leetcode】【python】Permutations
- 模拟-Codeforces 777C
- 通过spring boot构建solr查询实例
- 仿真三件套
- 克拉美罗下界 CRLB的计算
- Leetcode 122 Best Time to Buy and Sell Stock II
- linux下动态库(共享库)
- 使用foreach操作数组
- HDU 5977 树的分治+子集枚举