zoukankan      html  css  js  c++  java
  • SubsetsTotal Accepted:49746Total Submissions:176257My Submissions

    Subsets

    Total Accepted: 49746 Total Submissions: 176257My Submissions
    Given a set of distinct integers, nums, return all possible subsets.
    Note:
    Elements in a subset must be in non-descending order.
    The solution set must not contain duplicate subsets.
    For example,
    If nums = [1,2,3], a solution is:
    [
    [3],
    [1],
    [2],
    [1,2,3],
    [1,3],
    [2,3],
    [1,2],
    []
    ]

    package hashmap;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class test {
    
     public static void main(String[] args) {
      // TODO Auto-generated method stub
     int[] s={1,2,3};
     ArrayList<ArrayList<Integer>> result=subsets(s);
    
     for ( int i = 0; i < result.size(); i++){
     
     
      System.out.println(result.get(i));
      }
    }
    public static void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> l, int[] s, int pos){
     System.out.println("total pos:"+pos);
        if(pos == s.length){
            res.add(new ArrayList(l));
            System.out.println("----------------------");
            for ( int i = 0; i < res.size(); i++)
            System.out.println("res"+res.get(i));
            System.out.println("----------------------");
            return ;
        }
       
        dfs(res, new ArrayList(l), s, pos+1);
        l.add(s[pos]);
        System.out.println("addelem:"+pos+":"+s[pos]);
        System.out.println("dfs infor:"+pos);
        dfs(res,l, s, pos+1);
        System.out.println("l----------------------");
         for ( int k = 0; k < l.size(); k++){
     
      System.out.println(l.get(k));
      }
         
         System.out.println("l----------------------"); System.out.println("l----------------------");
    }
    
    
    public static ArrayList<ArrayList<Integer>> subsets(int[] S) {    
     ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> l = new ArrayList<Integer>();
    
            Arrays.sort(S);
         
            dfs(res, l, S, 0);
            return res;
    }
    
    
    }
    
    ------------------------------------------------------------------------------------------------------------------------------本娃的学习日记@lily园
  • 相关阅读:
    mtext中的las参数的作用
    并行与CPE
    根据局部性得出最优矩阵乘法写法
    cache中的thrashing问题和应对办法
    csapp(3e)的bomblab的phase_6详解(没有详细到逐行解析的程度)
    计划
    遇到问题怎么处理?
    数据对齐的几问
    python进阶(八、mysql:完整性约束)
    python进阶(七、mysql:表操作、数据操作、数据类型)
  • 原文地址:https://www.cnblogs.com/yanglicyfsdm/p/4710368.html
Copyright © 2011-2022 走看看