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 ...
随机推荐
- SQL server 2008里面通过sys.dm_exec_procedure_stats得到存储过程的执行信息--转
--转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/13/sql-server-2008-sys-dm-exec-procedure-stats. ...
- 解决github push错误The requested URL returned error: 403 Forbidden while accessing(转)
github push错误: git push error: The requested URL returned error: 403 Forbidden while accessing https ...
- 【Linux】mkdir命令
用途 mkdir命令主要是用来建立目录的 全称 mkdir的全称为:Make Directory 参数 -m :配置文件的权限 -p :帮助你直接将所需要的目录递归建立起来 案例 进入到目录/usr/ ...
- 名词解释:alpha版、beta版、rc版的意思(转)
很多软件在正式发布前都会发布一些预览版或者测试版,一般都叫“beta版”或者 “rc版”,特别是开源软件,甚至有“alpha版”,下面来解释一下各个版本的意思. alpha版:内部测试版.α是希腊字母 ...
- Linux-软件包管理-源码包安装
rpm -q gcc 查看c语言编译器是否已经安装 在浏览器输入:http://mirror.bit.edu.cn/apache/httpd/ 下载2.2.29这个包 cd ~ 回到root目录 ls ...
- Linux命令-权限管理命令:umask
umask -S 显示用户创建目录或文件时的默认权限 mkdir shuaige 创建一个shuaige目录 ls -ld shuaige 查看shuaige目录当前的权限(和上面默认的权限是一样的) ...
- 完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...
- powerdesigner 画PDM
一.PDM概述 PDM(物理数据模型-Physical Data Modal),通俗地理解,就是在PowerDesigner中以图形化的方式展示和设计数据库. PDM中涉及到的基本概念包括: 表: 列 ...
- 给Editplus去掉.bak文件
Tools-->Configure User Tools-->Files-->去掉create bacup file when saving前复选框的对号.
- 阿里云 部署并开启nodejs应用
1.下载资源 $ wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.xz 2.xz解压 $ xz -d node-v8. ...