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 (组合)的更多相关文章

  1. [leetcode]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  2. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  3. [LeetCode] 77. Combinations 全组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  4. LeetCode 77,组合挑战,你能想出不用递归的解法吗?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...

  5. Leetcode 77, Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. [FollowUp] Combinations 组合项

    这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...

  7. 【LeetCode】39. 组合总和

    39. 组合总和 知识点:递归:回溯:组合:剪枝 题目描述 给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数  ...

  8. LeetCode 77. 组合(Combinations)

    题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

  9. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

随机推荐

  1. Java lock 能被中断, synchronized 不能被中断

    1.lock是可中断锁,而synchronized 不是可中断锁 线程A和B都要获取对象O的锁定,假设A获取了对象O锁,B将等待A释放对O的锁定, 如果使用 synchronized ,如果A不释放, ...

  2. windows bat启动多个应用程序

      windows bat启动多个应用程序 CreationTime--2018年7月26日11点02分 Author:Marydon 1.应用场景 每天开机后,都需要打开平常所需要的软件,又不想将程 ...

  3. Weex Ui 是一个基于 Weex 的富交互、轻量级、高性能的移动端 UI 组件库

    Github资源:https://github.com/alibaba/weex-ui 预览 你可以通过飞猪.淘宝.天猫.Weex Playground 或者浏览器扫码体验 安装 npm i weex ...

  4. moment

    var now = moment(1410181234567)var formatted = now.format('YYYY-MM-DD HH:mm:ss')console.log(formatte ...

  5. IT行业面试指导 计算机行业面试技巧 面试技巧

    简历篇 简历是你的的第一张脸,简历写的是否合理,是否吸引人,将决定你能否赢得宝贵的面试机会,迈出第一步! l  姓名,性别,学历,居住地,工作年限,邮箱,手机号 l  填“现居住地”,不要填成“户籍所 ...

  6. js基本知识6

    1.2 复习 1. 节点 网页是有很多的节点组成的 . 元素节点 指的是 : 标签 li span 文本节点 属性节点 父子兄弟 父 parentNode nextSibling 孩子 childNo ...

  7. SecureCRT配置自动保存日志(实用)

    点“选项”---“全局选项”--“全局选项”--“默认会话”--“编辑默认设置”--“日志文件” 在“日志文件”中输入相应的参数就能达到这一效果. 比如你的日志文件放在的D:/SecureCRT/lo ...

  8. 使用Crypto++库的CBC模式实现加密(二)

    前面已经有一篇介绍使用Crypto++库实现的加密的文章了,但是代码中考虑的不完全,所以就重新发了个二 C++封装: #include "zyaes.h" #include < ...

  9. python 将base64字符串还原成图片保存

    import os,base64 strs='''/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCI ...

  10. jquer WdatePicker 使用 手册

    1. 跨无限级框架显示 无论你把日期控件放在哪里,你都不需要担心会被外层的iframe所遮挡进而影响客户体验,因为My97日期控件是可以跨无限级框架显示的 示例2-7 跨无限级框架演示 可无限跨越框架 ...