可以用类似于DFS的方法去做。这道题目是LC78 Subsets的子问题。

对于LC78,另一个巧妙的做法是:我们可以先用一个整形数组(长度和nums相等),数组元素一开始都是1。把整个数组看成一个二进制数,然后模拟二进制减法,将这个数组一步一步减1减到0。每减一次1,对照这个二进制数组和nums数组,如果某个位置上二进制数组元素为1,则将nums数组相应位置上的数输出。这样就能输出所有的子集。

这里附上LC77的代码。

 1 class Solution {
 2 private:
 3     vector<vector<int> > ret;
 4     vector<int> a;
 5 public:
 6     void solve(int dep, int maxDep, int n, int start)
 7     {
 8         if (dep == maxDep)
 9         {
10             ret.push_back(a);
11             return;
12         }
13         int last=n+1-(maxDep-dep);
14         for(int i = start; i <= last ; i++)
15         {
16             a[dep] = i;
17             solve(dep + 1, maxDep, n, i + 1);
18         }
19     }
20
21     vector<vector<int> > combine(int n, int k) {
22         a.resize(k);
23         ret.clear();
24         solve(0, k, n, 1);
25         return ret;
26     }
27 };

View Code

转载于:https://www.cnblogs.com/vaecn/p/5349981.html

LC77 Combinations相关推荐

  1. [Leetcode] Combinations 组合数

    Combinations Given two integers n and k, return all possible ombinations of k numbers out of 1 ... n ...

  2. LeetCode 17. Letter Combinations of a Phone Number--笔试题--C++,Python解法

    题目地址:Letter Combinations of a Phone Number - LeetCode Given a string containing digits from 2-9 incl ...

  3. LeetCode-17-Letter Combinations of a Phone Number

    算法描述: Given a string containing digits from 2-9 inclusive, return all possible letter combinations t ...

  4. itertools库 combinations() 和 permutations() 组合 和 排列选项的方法

    combinations方法重点在组合,permutations方法重在排列. combinations和permutations返回的是对象地址,原因是在python3里面,返回值已经不再是list ...

  5. LeetCode Combinations (组合)

    问题:给出n,k,要求输出从1到n选取k个数的所有组合 思路:使用dfs,第一层是从1到n中选取一个数,第二层是从基于上一层选取的数+1到n中选取一个数,依次类推. 具体代码参考: https://g ...

  6. leetcode算法题--Combinations

    原题链接:https://leetcode.com/problems/combinations/ class Solution {public:vector<vector<int>& ...

  7. Combinations

    Combinations Total Accepted: 10949 Total Submissions: 36507My Submissions Given two integers n and k ...

  8. Combinations leetcode java

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  9. Python -itertools模块combinations方法

    itertools模块combinations(iterable, r)方法可以创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序. 例1 ...

最新文章

  1. 微服务落地,我们在考虑什么?
  2. 软件工程方法论对我们开发软件有多大用处?谈谈你的看法。
  3. Java poi插件导出Excel文件合并多sheet页
  4. java是什么_Java是什么?
  5. 中文分词之正向最大匹配算法
  6. 使用循环计算斐波那契数列
  7. VS Code修改菜单栏字体大小
  8. Android 目录
  9. Nunit中如何进行事务性单元测试
  10. kafka基础之介绍和分布式集群搭建
  11. 效果直逼flash的Div+Css+Js菜单
  12. Excel的裁剪(trimming)(トリミング)功能使用介绍
  13. linux系统下idea打包apk教程,intellij idea14打包apk文件和查看sha1值
  14. 武汉理工计算机专考研专业课,2017武汉理工大学考研计算机专业课真题
  15. 苹果呼叫转移设置不了_0广告,支持苹果,甜美小姐姐or磁性小哥哥做助理!
  16. 计算机多窗口显示桌面,怎么同时电脑桌面打开多个excel窗口
  17. html是l面包屑效果,CSS制作面包屑
  18. 华南理工计算机就业棒棒,国内3所校名“一字之差”的大学:均为理工大学,实力却天差地别...
  19. kafka 报错: IllegalArgumentException: Error creating broker listeners from ‘PLAINTEXT:xxx.xxx.xxx.xx
  20. Maven下载及目录结构

热门文章

  1. 图解LanHelper 使用
  2. ctrl+Enter 自动加上 .com 而不是 .com.cn
  3. 致年轻的创业者:人脉没那么重要
  4. RocketMQ最佳实战
  5. mysql知识点回顾(一)
  6. CMS模板应用调研问卷
  7. 移动文件流的读写指针---fseek
  8. CentOS6.5菜鸟之旅:安装VirtualBox4.3
  9. Mongo报如下类似错误时的修改方法Cannot natively represent the long 1396367483000 on this platform...
  10. 使用Windows的分析等待链(analyze wait chain)来诊断没用响应的应用