2 0 1 9 -6-12 星 期三 开 始 吧
上 一 题 链 接 Leetcode基础刷题之 PHP 解析(47. Permutations II)
题 目 描 述
给定两个整型数字n和k,返回1到n的k个数所有组合情况。
题 目 分 析
还是和之前的题目一样,定义了两个数组,一个存储单次的组合,一个存储最后的所有组合,那么每次当前组合中的个数等于k的时候,就把当前小组合push到大组合中,否则的话继续递归。
/**
* @param Integer $n
* @param Integer $k
* @return Integer[][]
*/
function combine($n, $k) {
$res=[];
$out=[];
$this->helper($n,$k,1,$out,$res);
return $res;
}
function helper($n,$k,$level,&$out,&$res){
if(count($out)==$k){
array_push($res,$out);
return ;
}
for($i=$level;$i<=$n;$i++){
array_push($out,$i);
$this->helper($n,$k,$i+1,$out,$res);
array_pop($out);
}
}
Github整理地址 : https://github.com/wuqinqiang/leetcode-php
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Apache Flink 零基础入门(一):基础概念解析
- Apache Flink 零基础入门(一):基础概念解析
- JStorm 源码解析:基础线程模型
- React Hooks 解析(上):基础
- TypeScript基础入门之模块解析(一)
- TypeScript基础入门之模块解析(二)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Graph Theory
Douglas B. West / Prentice Hall / 2000-9-1 / USD 140.00
For undergraduate or graduate courses in Graph Theory in departments of mathematics or computer science. This text offers a comprehensive and coherent introduction to the fundamental topics of graph ......一起来看看 《Introduction to Graph Theory》 这本书的介绍吧!
URL 编码/解码
URL 编码/解码
RGB CMYK 转换工具
RGB CMYK 互转工具