zoukankan      html  css  js  c++  java
  • 跳水板

    你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。

    返回的长度需要从小到大排列。

    示例 1

    输入:

    shorter = 1
    longer = 2
    k = 3
    输出: [3,4,5,6]
    解释:
    可以使用 3 次 shorter,得到结果 3;使用 2 次 shorter 和 1 次 longer,得到结果 4 。以此类推,得到最终结果。
    提示:

    0 < shorter <= longer
    0 <= k <= 100000

    code1:递归

    class Solution {
    private:
        void divingBoardCore(const int shorter, const int longer, int k, int k1, vector<int>& res){
            if(k<0)
                return;
            res.push_back(shorter*k+longer*k1);
            divingBoardCore(shorter, longer, k-1, k1+1, res);
        }
    public:
        vector<int> divingBoard(int shorter, int longer, int k) {
            vector<int> res;
            if(k<=0){
                return res;
            }
    
            if(shorter == longer){
                res.push_back(shorter*k);
                return res;
            }
    
            divingBoardCore(shorter, longer, k, 0, res);
            return res;
        }
    };

    code2:循环

    class Solution {
    public:
        vector<int> divingBoard(int shorter, int longer, int k) {
            vector<int> res;
            if(k<=0){
                return res;
            }
    
            if(shorter == longer){
                res.push_back(shorter*k);
                return res;
            }
    
            res.push_back(k*shorter);
            for(int i=k-1; i>=0; --i){
                res.push_back(i*shorter+(k-i)*longer);
            }
            return res;
        }
    };
  • 相关阅读:
    转载—javascript 设计模式 文章很长,请自备瓜子,水果和眼药水
    js 中call()方法的使用
    上传、下载
    steps1>Struct2配置文件
    页面刷新
    steps1>Struct2控制器组件
    steps1>Struct2概述
    steps1>Struct2基本流程
    steps1>Struct2struts.xml
    steps1>Struct2web.xml
  • 原文地址:https://www.cnblogs.com/tianzeng/p/13339895.html
Copyright © 2011-2022 走看看