面试题 16.11. 跳水板(leetcode)-7月8日

题目

面试题 16.11. 跳水板(leetcode)

我的思路

数学方法,一共k+1种情况:使用shorter0次到k次。一个循环把i*shorter+(k-i)*longer,0<=i<=k算一遍即可。

我的实现

class Solution {
public:
    vector<int> divingBoard(int shorter, int longer, int k) {
        vector<int> board;
        if(k==0) return board;
        int temp;
        for(int i=0;i<=k;i++){
            temp = (k-i)*shorter+i*longer;
            if(i==0)
            board.push_back(temp);
            else if(temp>board.back())
            board.push_back(temp);
        }
        return board;
    }
};

注意考虑特殊情况:

  1. k=0,木板总数为0
  2. shorter=longer,长度只有一种取值

拓展学习

vector的使用和实现原理

原文地址:https://www.cnblogs.com/BoysCryToo/p/13265335.html