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 ...
随机推荐
- 百度地图API拾取坐标网址
http://api.map.baidu.com/lbsapi/getpoint/index.html
- 每日算法之二十三:Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- JSP 九大隐式对象
final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; fina ...
- 流媒体协议RTMP,RTSP与HLS有什么不同
转载自:http://www.cuplayer.com/player/PlayerCode/Wowza/2015/0204/1774.html HLS (HTTP Live Streaming) Ap ...
- 常见URL字符及URL编码值
大家上网的时候一定会看到很多这类情况有的网页地址都是%22%32%11%23%21等这种机器语言恐怕只有机器能马上辨认吧现在我把大概知道的总结一下 字符 ...
- unity, instantiate一个实例后,先指定parent,再指定position
instantiate一个实例后,先指定parent,再指定position,才能保证position正确,如果先指定position再指定parent,则position会错误.
- Angularjs Directive - Compile vs. Link
如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应. 输入 hellobug 失去 ...
- Atitit.故障排除系列-----apache 不能启动的排除
Atitit.故障排除系列-----apache 不能启动的排除 能直接使用cli启动httpd ,,详细打印出信息.. C:\Users\ASIMO>"C:\wamp\apach ...
- Decoration7:注册登录设计
首先顶一个小目标:从前台写入用户数据后,登录的时候输入用户名和密码,能够查询数据库成功,同时记录session数据 1.Login界面,为了只关注主线任务,我直接套用一个bootstrap模板admi ...
- SAP ECC6安装系列一:安装前硬件和软件准备
原作者博客 http://www.cnblogs.com/Michael_z/ ======================================== 写在前面的罗嗦话 一晃就是5年,前几天 ...