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
  • 相关阅读:
    10000000000
    vue生命周期
    react基础
    第一个react
    vuex状态管理2
    vue配合UI组件
    vuex
    vue-router配合vue-cli的实例
    vue-router2.0
    父子组件2.0
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4328264.html
Copyright © 2011-2022 走看看