TreeviewCopyright © aleen42 all right reserved, powered by aleen42
78. 子集
https://leetcode-cn.com/problems/subsets/
Java
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
for(int i = 0; i < nums.length; i++) {
int all = result.size();
for(int j = 0; j < all; j++) {
List<Integer> temp = new ArrayList<>(result.get(j));
temp.add(nums[i]);
result.add(temp);
}
}
return result;
}
}
Python
'''
Author: Goog Tech
Date: 2020-10-27 14:18:56
LastEditTime: 2020-10-27 14:20:21
Description: https://leetcode-cn.com/problems/subsets/
FilePath: \leetcode-googtech\#78. Subsets\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = [[]]
for i in nums:
result = result + [[i] + num for num in result]
return result