[LeetCode]题解(python):078-Subsets
2015-12-30 13:20
609 查看
[b]题目来源:[/b]
https://leetcode.com/problems/subsets/
[b]题意分析:[/b]
给定一个没有重复数字的数组,返回他所有的子集。子集不能重复,子集中的元素要按照字典序排序。
[b]题目思路:[/b]
这道题也是用递归的思想。长度为n的数组a的所有子集是数组a[:n-1]的所有子集并上其子集中每个元素append(a[n-1]).要元素按照字典序排序,首先要将数组排序。
[b]代码(Python):[/b]
View Code
转载请注明出处:http://www.cnblogs.com/chruny/p/5088529.html
https://leetcode.com/problems/subsets/
[b]题意分析:[/b]
给定一个没有重复数字的数组,返回他所有的子集。子集不能重复,子集中的元素要按照字典序排序。
[b]题目思路:[/b]
这道题也是用递归的思想。长度为n的数组a的所有子集是数组a[:n-1]的所有子集并上其子集中每个元素append(a[n-1]).要元素按照字典序排序,首先要将数组排序。
[b]代码(Python):[/b]
class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ def subset(nums,index): if index == 0: return [[]] tmp = subset(nums,index - 1) ans = tmp[:] for i in tmp: ans.append(i + [nums[index -1]]) return ans nums.sort() return subset(nums,len(nums))
View Code
转载请注明出处:http://www.cnblogs.com/chruny/p/5088529.html
相关文章推荐
- [LeetCode]题解(python):077-Combinations
- Python 第二章 函数
- [LeetCode]题解(python):076-Minimum Window Substring
- caffe 如何调用python层
- Caffe学习系列(13):数据可视化环境(python接口)配置
- python AES-16位加密解密功能实现
- Python 获取Twitter用户与Friends和Followers的关系(eg, 交集,差集)
- 仿射变换(Affine transformation)与python实践
- python 字典排序
- python手记(51)
- Python 1.6 使用dict和set
- [python] 专题八.多线程编程之thread和threading
- python模拟登陆和发布信息
- python Tab自动补全命令设置
- 4. Median of Two Sorted Arrays leetcode python New season for 2016
- 【python】多个文件共用日志系统的重复打印问题
- C#中通过Process运行Python脚本
- leetcode之Binary Tree Paths
- Python 1.5 循环
- leetcode之Sum Root to Leaf Numbers