leetCode 77.Combinations (组合)
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],
]
思路:此题意思是给一个n,和k,求1-n的k个数的组合。没有反复(位置交换算反复)。可用排列组合的统一公式求解。
代码例如以下:
public class Solution {
boolean[] f;
List<List<Integer>> list;
public List<List<Integer>> combine(int n, int k) {
list = new ArrayList<List<Integer>>();
if(k > n || k < 1){//必须是有效k值
return list;
}
f = new boolean[n];
int[] a = new int[n];
for(int i=0; i < n; i++){
a[i] = i+1;//将数填充
}
mm = n-1;
count(a,"",k,n,0);//调用函数
return list;
}
/**
* 实现对k个数字的排练组合
* @param a 数组
* @param s 排练组合得到的结果
* @param nn 排练组合的数字个数
*/
int kk = 0;
int mm;
private void count(int[] a,String s,int nn,int n,int j){
if(nn == 0){//处理结果
String[] sa = s.split(",");//切割数组
List<Integer> al = new ArrayList<Integer>();
for(int i = 0;i < sa.length; i++){
if(sa[i].length() > 0)//仅仅有当不为空的时候才加入
al.add(Integer.parseInt(sa[i]));//加入
}
list.add(al);
return;
}
//遍历,从i=j開始,仅仅要i开头的与i大的值
for(int i = j; i < a.length; i++){
if(!f[i]){
f[i] = true;
count(a,s + "," + a[i],nn-1,n,i+1);//调用下一个为false的数字
f[i] = false;
}
}
}
}
leetCode 77.Combinations (组合)的更多相关文章
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode 77,组合挑战,你能想出不用递归的解法吗?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...
- Leetcode 77, Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- 【LeetCode】39. 组合总和
39. 组合总和 知识点:递归:回溯:组合:剪枝 题目描述 给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 ...
- LeetCode 77. 组合(Combinations)
题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- 谁占用了我的Buffer Pool?--【转】
转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/11/buffer-pool.aspx 我在做SQL Server 7.0技术支持的时候有客户问我 ...
- Android中的Layout_weight终极研究
以前在做UI布局时,也经常用Layout_weight属性,有时会遇到莫名其妙的布局问题,但总没研究懂.一直想做深入分析,但总是没耐心.遇到问题就找替代方法解决,但终非长久之计.这次下决心给它弄透! ...
- HTML5学习笔记 音频
HTML5提供了播放音频的标准. Web上的音频 直到现在,仍然不存在一项旨在网页上播放音频的标准. 今天,大多数音频是通过插件比如flash来播放的.然而,并非所有的浏览器都拥有同样的插件. hmt ...
- jQuery开发插件的两种方式
最近挺多人写jQuery的,都是关于jQuery扩展方面的,使用方面的讲的比较多,但是关于详细的一个基础的过程讲的比较少一点,做web开发的基本上都会用到jQuery,本人就根据jQuery的使用经验 ...
- 摘:常用函数(包括:宽字符函数、普通C函数 )
只要看见“W”就是宽的意思,左边wchar_t,右边char 字符分类: 宽字符函数 普通C函数 描述 iswalnum() isa ...
- 磁盘io和吞吐量
磁盘的 IOPS,也就是在一秒内,磁盘进行多少次 I/O 读写. 磁盘的吞吐量,也就是每秒磁盘 I/O 的流量,即磁盘写入加上读出的数据的大小. IOPS 与吞吐量的关系每秒 I/O 吞吐量= IOP ...
- SQL 关于apply的两种形式cross apply 和 outer apply, with cube 、with rollup 和 grouping
1). apply有两种形式: cross apply 和 outer apply先看看语法: <left_table_expression> {cross|outer} apply &l ...
- XMLDocument转为String 摘录
public static string FormatXmlString(string xmlString) { XmlDocument document = new XmlDocument(); d ...
- linode使用apt更新时停止的错误
使用ubuntu在更新系统时候,有时候会出现这样的问题: 0% [Connecting to security.ubuntu.com (2001:67c:1560:8001::14)] 问了下客户,他 ...
- THREADSPOOL
STPStartInfo stp = new STPStartInfo();//线程详细配置参数 stp.CallToPostExecute = CallToPostExecute.Always;// ...