LeetCode Permutaions
来源:互联网 发布:完美假期王蒙淘宝店 编辑:程序博客网 时间:2024/06/09 23:29
LeetCode解题之Permutaions
原题
输出一个没有重复数字的数组的全排列。
注意点:
- 不用考虑重复数字的情况
例子:
输入: nums = [1, 2, 3]
输出: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
解题思路
这道题比较单间,采用递归把数组中的数字依次加入当前数组current来进行排列组合。
AC源码
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: result.append(current + []) return for i, v in enumerate(num): current.append(num[i]) self.get_permute(current, num[:i] + num[i + 1:], result) current.pop()if __name__ == "__main__": assert Solution().permute([1, 2, 3]) == [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。
0 0
- LeetCode Permutaions
- LeetCode Permutaions II
- LeetCode刷题笔录 Permutaions
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- AsyncTask
- 1、Two Sum
- windows8.1+ubuntu 14.04双系统安装
- 输入一行字符串,把字符串翻转[单词顺序不变]
- 5.UITableView基本使用
- LeetCode Permutaions
- 菜单 PHP 开发中的外围资源性能分析(一)
- Windows2003终端服务器超出了最大允许连接数
- MySQL下创建索引
- Log4j使用指南
- Android sdk 下载链接
- INSERT IGNORE 与INSERT INTO的区别
- 全极化SAR图像freeman分解
- Makefile经典教程(掌握这些足够)