• 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园
  • 相关阅读:
    pwd命令
    python-windows环境安装
    python介绍
    elk安装
    elk介绍
    111
    使用CEF作为用户界面
    使用CEF作为浏览器
    c# 内嵌chrome(Webkit)
    待搞清楚
  • 原文地址:https://www.cnblogs.com/yanglicyfsdm/p/4710368.html
Copyright © 2020-2023  润新知