zoukankan      html  css  js  c++  java
  • Subsets

    Subsets

    问题:

    Given a set of distinct integers, S, return all possible subsets.

    Note:

    • Elements in a subset must be in non-descending order.
    • The solution set must not contain duplicate subsets.

    思路:

      DFS + 回溯

    我的代码:

    public class Solution {
        public List<List<Integer>> subsets(int[] S) {
            if(S == null || S.length == 0)  return rst;
            List<Integer> list = new ArrayList<Integer>();
            Arrays.sort(S);
            helper(list, S, 0);
            return rst;
        }
        private List<List<Integer>> rst = new ArrayList<List<Integer>>();
        public void helper(List<Integer> list, int[] candidates, int start)
        {
            rst.add(new ArrayList(list));
            for(int i = start ; i < candidates.length; i++)
            {
                list.add(candidates[i]);
                helper(list, candidates, i + 1);
                list.remove(list.size() - 1);
            }
        }
    }
    View Code
  • 相关阅读:
    Python内置函数(22)——list
    Git在不同环境换行符设置
    Spring之AOP
    Spring之IOC
    Spring--框架简介
    git-远程协作
    git-SSH连接配置
    git-本地操作
    git简介
    浅谈Sql各种join的用法
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4328264.html
Copyright © 2011-2022 走看看