【LeetCode】C# 77、Combinations
来源:互联网 发布:电脑绑定mac地址 编辑:程序博客网 时间:2024/06/05 15:05
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
给定两个值k,n,返回用n个数字中的k个形成的所有排列组合。
思路:通过迭代,遍历n个数,依次往temp数组中添加元素直到元素个数为k在添加到result的数组组。
public class Solution { public List<List<int>> Combine(int n, int k) { List<List<int>> combs = new List<List<int>>(); combine(combs, new List<int>(), 1, n, k); return combs; } public static void combine(List<List<int>> combs, List<int> comb, int start, int n, int k) { if(k==0) { combs.Add(new List<int>(comb)); return; } for(int i=start;i<=n;i++) { comb.Add(i); combine(combs, comb, i+1, n, k-1); comb.RemoveAt(comb.Count-1); } }}
阅读全文
0 0
- 【LeetCode】C# 77、Combinations
- LeetCode 77 Combinations
- [leetcode 77] Combinations
- Leetcode NO.77 Combinations
- [leetcode] 77 Combinations
- [LeetCode 77]Combinations
- leetcode || 77、Combinations
- leetcode-77 Combinations
- LeetCode-77 Combinations(组合)
- Combinations - LeetCode 77
- leetcode 77:Combinations
- LeetCode(77) Combinations
- Leetcode #77 Combinations
- [leetcode-77]Combinations(java)
- leetcode 77: Combinations
- LeetCode(77) Combinations
- 【leetcode】【77】Combinations
- leetcode 77:Combinations
- 设计模式(7)--适配器模式
- 学习博客
- spring 3以及之后版本的异步方法
- 単例模式(设计模式)
- docker以及docker-compose 使用加速器部署
- 【LeetCode】C# 77、Combinations
- Ubuntu 11.10下将图形用户界面改为Gnome
- IAR 10 版查看目标生成代码量的方法
- 解决CentOS5.5无法使用ifconfig命令
- Herosphp 3.0.1 发布,轻量级php开源框架
- APIGateway 简介
- VS2010调试的时候怎么看函数返回值
- UVALive 7501Business Cycle(二分,xjb乱判)
- LeetCode 105,106. Construct Binary Tree 重建二叉树 Python Solution