zoukankan      html  css  js  c++  java
  • 216. 组合总和 III

    地址:https://leetcode-cn.com/problems/combination-sum-iii/

    <?php
    /**
     * Created by PhpStorm.
     * User: huahua
     * Date: 2020/10/21
     * Time: 下午4:54
    216. 组合总和 III
    找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
    
    说明:
    
    所有数字都是正整数。
    解集不能包含重复的组合。
    示例 1:
    
    输入: k = 3, n = 7
    输出: [[1,2,4]]
    示例 2:
    
    输入: k = 3, n = 9
    输出: [[1,2,6], [1,3,5], [2,3,4]]
     */
    class Solution {
    
        /**
         * @param Integer $k
         * @param Integer $n
         * @return Integer[][]
         */
        protected $ans = [];
        public function combinationSum3($k, $n)
        {
            $this->backtrack($k, $n, 1, []);
            return $this->ans;
        }
    
        private function backtrack($k, $target, $start, $path)
        {
            if ($target == 0 && count($path) == $k) {
                $this->ans[] = $path;
                return;
            }
    
            for ($i = $start; $i <= 9; ++$i) {
                if ($i > $target) continue;
                if (count($path) >= $k) continue;
                $path[] = $i;
                $this->backtrack($k, $target - $i, $i + 1, $path);
                array_pop($path);
            }
        }
    }
  • 相关阅读:
    leetcode——91.解码方法
    leetcode——64.最小路径和
    Layui上传图片2.0版
    LINQ中判断日期时间段
    Http基础
    Js中数组,字符串的常用方法
    C#数组,ArrayList,List区别
    08-01 通过线性回归了解算法流程
    08-00 课程习得
    C-02 推荐系统
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/13853355.html
Copyright © 2011-2022 走看看