lintcode 中等题:subSets 子集
生活随笔
收集整理的這篇文章主要介紹了
lintcode 中等题:subSets 子集
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
子集?
給定一個含不同整數的集合,返回其所有的子集
樣例如果 S =?[1,2,3],有如下的解:
[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[] ] 注意子集中的元素排列必須是非降序的,解集必須不包含重復的子集
挑戰你可以同時用遞歸與非遞歸的方式解決么?
解題
根據上面求排列的思想很類似,還是深度優先遍歷。由于輸出每個子集需要升序,所以要先對數組進行排序。求出所以的子集,也就是求出所以的組合方式 + 空集
問題轉化為求組合方式的問題
參考鏈接不僅要考慮起始位置,還需要考慮長度,這樣才是組合 C(n,k),由于我只想到要考慮起始位置,而長度問題在程序中增加,一直沒有解決問題
核心程序
public void helper(int[] nums,int start,int len,ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){if( list.size() == len){res.add(new ArrayList<Integer>(list));return;}for(int i=start;i< nums.length;i++){if(list.contains(nums[i])){continue;}list.add(nums[i]);helper(nums,i+1,len,list,res);list.remove(list.size()-1);}} class Solution {/*** @param S: A set of numbers.* @return: A list of lists. All valid subsets.*/public ArrayList<ArrayList<Integer>> subsets(int[] nums) {// write your code hereArrayList<Integer> list = new ArrayList<Integer>();ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();if(nums == null || nums.length ==0)return res;Arrays.sort(nums);res.add(list);for(int len = 1;len<= nums.length;len++){helper(nums,0,len,list,res);}return res;}public void helper(int[] nums,int start,int len,ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){if( list.size() == len){res.add(new ArrayList<Integer>(list));return;}for(int i=start;i< nums.length;i++){if(list.contains(nums[i])){continue;}list.add(nums[i]);helper(nums,i+1,len,list,res);list.remove(list.size()-1);}} } Java Code九章中程序進行了優化,長度不考慮,遞歸一次list的值都是子集的一個元素
class Solution {/*** @param S: A set of numbers.* @return: A list of lists. All valid subsets.*/public ArrayList<ArrayList<Integer>> subsets(int[] nums) {// write your code hereArrayList<Integer> list = new ArrayList<Integer>();ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();if(nums == null || nums.length ==0)return res;Arrays.sort(nums);helper(nums,0,list,res);return res;}public void helper(int[] nums,int start, ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){res.add(new ArrayList<Integer>(list));for(int i=start;i< nums.length;i++){if(list.contains(nums[i])){continue;}list.add(nums[i]);helper(nums,i+1,list,res);list.remove(list.size()-1);}} } Java Code class Solution:"""@param S: The set of numbers.@return: A list of lists. See example."""def subsets(self, S):def dfs(depth, start, valuelist):res.append(valuelist)if depth == len(S): returnfor i in range(start, len(S)):dfs(depth+1, i+1, valuelist+[S[i]])S.sort()res = []dfs(0, 0, [])return res Python Code根據位運算進行求解
// 1 << n is 2^n// each subset equals to an binary integer between 0 .. 2^n - 1// 0 -> 000 -> []// 1 -> 001 -> [1]// 2 -> 010 -> [2]// ..// 7 -> 111 -> [1,2,3]?下面是我自己實現
class Solution {/*** @param S: A set of numbers.* @return: A list of lists. All valid subsets.*/public ArrayList<ArrayList<Integer>> subsets(int[] nums) {// write your code hereint len = nums.length;ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();if(nums == null || len==0)return res;Arrays.sort(nums);// 1 << n is 2^n// each subset equals to an binary integer between 0 .. 2^n - 1// 0 -> 000 -> []// 1 -> 001 -> [1]// 2 -> 010 -> [2]// ..// 7 -> 111 -> [1,2,3]for(int i=0;i< 1<<len ;i++){ArrayList<Integer> list = new ArrayList<Integer>();// 檢測哪一位是 1 int n = i;for(int j=0;j< len;j++){if(n%2==1)list.add(nums[j]);n=n/2;}res.add(list);}return res;} } View Code九章中判斷第幾位是1的程序如下:
for (int i = 0; i < (1 << n); i++) {ArrayList<Integer> subset = new ArrayList<Integer>();for (int j = 0; j < n; j++) {// check whether the jth digit in i's binary representation is 1if ((i & (1 << j)) != 0) {subset.add(nums[j]);}}result.add(subset);}是懂非懂,好像這樣也可以判斷第幾位是1
class Solution:"""@param S: The set of numbers.@return: A list of lists. See example."""def subsets(self, nums):# write your code hereres = []size = len(nums)nums.sort()if nums == None or size == 0:return resfor i in range(1<<size):lst=[]n = ifor j in range(size):if n%2==1:lst.append(nums[j])n/=2res.append(lst)return res Python Code?
?
轉載于:https://www.cnblogs.com/theskulls/p/5099745.html
總結
以上是生活随笔為你收集整理的lintcode 中等题:subSets 子集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring 学习笔记 3. 尚硅谷_佟
- 下一篇: 如何解决机器学习中数据不平衡问题