lintcode:数字组合III

news/2024/7/8 7:47:10

数字组合III

组给出两个整数n和k,返回从1......n中选出的k个数的组合。

样例

例如 n = 4 且 k = 2

返回的解为:

[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]] 

解题

数字组合I  数组组合II

同样式DFS

本题只需要增加判断size 是否等于k的情况

public class Solution {
    /**
     * @param n: Given the range of numbers
     * @param k: Given the numbers of combinations
     * @return: All the combinations of k numbers out of 1..n
     */
    public ArrayList<ArrayList<Integer>> combine(int n, int k) {
        // write your code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if(n<0 || k<0)
            return result;
        int start = 1;
        ArrayList<Integer> list = new ArrayList<Integer>();
        DFS(n,k,start,result,list);
        return result;
    }
    public void DFS(int n,int k,int start ,ArrayList<ArrayList<Integer>> result,ArrayList<Integer> list){
        if(list.size() == k){
            ArrayList<Integer> tmp = new ArrayList<Integer>(list);
            if(!result.contains(tmp)) // 可不要
                result.add(tmp);
            return;
        }
        for(int i = start;i<=n ;i++){
            list.add(i);
            DFS(n,k,i+1,result,list);
            list.remove(list.size() -1);
        }
    }
}

 

 

 


http://www.niftyadmin.cn/n/648272.html

相关文章

php数组递归函数返回值,PHP递归调用数组值并用其执行指定函数的方法

这篇文章主要介绍了PHP递归调用数组值并用其执行指定函数的方法,涉及php数组调用与函数执行的技巧,具有一定参考借鉴价值,需要的朋友可以参考下本文实例讲述了PHP递归调用数组值并用其执行指定函数的方法。分享给大家供大家参考。具体分析如下&#xff1a;以下为wordpress原代码…

php的mysql\mysqli\PDO(二)mysqli

原文链接&#xff1a;http://www.orlion.ga/1147/ mysqli有面向对象风格和面向过程风格&#xff0c;个人感觉还是用面向对象风格比较好(毕竟是面向对象) 1、mysqli::_construct() mysqli::__construct ([ string $host ini_get("mysqli.default_host") [, string $u…

Java IO: InputStreamReader和OutputStreamWriter

作者: Jakob Jenkov 译者: 李璟(jlee381344197gmail.com) 本章节将简要介绍InputStreamReader和OutputStreamWriter。细心的读者可能会发现&#xff0c;在之前的文章中&#xff0c;IO中的类要么以Stream结尾&#xff0c;要么以Reader或者Writer结尾&#xff0c;那这两个同时以字…

php 活动报名,活动报名小程序 - 微信小程版的活动在线报名,支持付费活动发布! – 基于ThinkPHP和Bootstrap的极速后台开发框架...

活动报名小程序是一款基于FastAdmin和ThinkPHP开发的活动报名小程序应用插件&#xff0c;通过PC后端可在线发布活动信息&#xff1b;可设置活动费用&#xff0c;活动时间&#xff0c;人数限制等等信息&#xff0c;同时还可以多模板颜色自定义等功能。功能特性1、幻灯管理可在线…

php 数据摘要,php摘要生成函数(无乱码)

在使用的时候&#xff0c;得先把要生成摘要的内容strip_tags()一下&#xff0c;当然&#xff0c;你也可以把strip_tags()直接添加到函数中&#xff0c;我没有搞&#xff0c;自己添加吧。下面是函数&#xff1a;function cutstr($string, $length,$charset,$dot) {//字符&#x…

php可以写系统吗,php – 文件系统不可写

我成功地在Web主机中安装了Drupal 7.安装模块后,出现错误错误,状态报告给出了以下错误消息&#xff1a;File system Not writableThe directory /Applications/MAMP/tmp/PHP does not exist.You may need to set the correct directory at the file system settings page orcha…

WordPress Import 上传的文件尺寸超过php.ini中定义的upload_max_filesize值--gt;解决方法。...

參考一&#xff1a; WordPress Importer上传导入备份文件时遇到这样一个错误&#xff0c;提示“上传的文件尺寸超过 php.ini 中定义的 upload_max_filesize 值”。问题原因是php.ini文件中限制了最大上传文件大小&#xff0c;默觉得2MB。解决方法找到这个文件依照下文方法略微改…

dba社区 mysql,Mysql B+树

B树https://www.cndba.cn/Supreme_Aaron/article/4442 B树https://www.cndba.cn/Supreme_Aaron/article/4442 Oracle用的B树。Mysql用的B树。https://www.cndba.cn/Supreme_Aaron/article/4442优缺点B树矮胖(相同数据情况下B树层次少。叶子节点多)>搜索起来快B树天然带排序。…