zoukankan      html  css  js  c++  java
  • lintcode-17-子集

    子集

    给定一个含不同整数的集合,返回其所有的子集

    注意事项

    子集中的元素排列必须是非降序的,解集必须不包含重复的子集

    样例

    如果 S = [1,2,3],有如下的解:

    [
    [3],
    [1],
    [2],
    [1,2,3],
    [1,3],
    [2,3],
    [1,2],
    []
    ]

    挑战

    你可以同时用递归与非递归的方式解决么?

    标签

    递归 脸书 优步

    code

    class Solution {
    public:
        /**
         * @param S: A set of numbers.
         * @return: A list of lists. All valid subsets.
         */
        vector<vector<int> > subsets(vector<int> &nums) {
        	// write your code here
            vector<vector<int> > result;
            int size = nums.size();
            
            if(size == 0) {
                result.push_back(vector<int> ()); 
                return result; 
            }
    
            sort(nums.begin(),nums.end());
    
            vector<int> temp;
            subset(result, nums, temp, 0, size);
    
            return result;
        }
    
        void subset(vector<vector<int> > &result, vector<int> nums, vector<int> temp, int begin, int end) {
            result.push_back(temp);
    
            for(int i=begin; i<end; i++) {
                temp.push_back(nums[i]);
                subset(result, nums, temp, i+1, end);
                temp.pop_back();
            }
        }
    };
    
  • 相关阅读:
    Docker搭建redis集群
    PHP中的OPCode和OPCache
    Redis的三种集群模式
    MySQL事务的隔离级别
    Docker镜像分层技术
    为什么 MongoDB 选择B树,Mysql 选择B+树?
    MongoDB的使用
    cesium+vue挖坑展示
    Ceium+Vue踩坑记录
    渲染总结——记录
  • 原文地址:https://www.cnblogs.com/libaoquan/p/6993506.html
Copyright © 2011-2022 走看看