zoukankan      html  css  js  c++  java
  • 078 Subsets 子集

    给定一组不同的整数 nums,返回所有可能的子集(幂集)。
    注意事项:该解决方案集不能包含重复的子集。
    例如,如果 nums = [1,2,3],结果为以下答案:
    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]
    详见:https://leetcode.com/problems/subsets/description/

    Java实现:

    class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> res=new ArrayList<List<Integer>>();
            List<Integer> out=new ArrayList<Integer>();
            Arrays.sort(nums);
            helper(nums,0,out,res);
            return res;
        }
        private void helper(int[] nums,int start,List<Integer> out,List<List<Integer>> res){
            res.add(new ArrayList<Integer>(out));
            for(int i=start;i<nums.length;++i){
                out.add(nums[i]);
                helper(nums,i+1,out,res);
                out.remove(out.size()-1);
            }
        }
    }
    
  • 相关阅读:
    享元模式及php实现
    共享内存
    LCD触屏驱动
    I2C驱动
    C++ & java小结
    使用GlobalKey启动APP
    socketpair通信
    inotify和epoll
    C语言之二叉树
    灯光系统
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8711458.html
Copyright © 2011-2022 走看看