leetcode 1518. 换酒问题(java & python3)

leetcode 1518. 换酒问题(java & python3),第1张

leetcode 1518. 换酒问题(java & python3)

java:

简单题重拳出击 快乐每日一题 

可以边喝边换 芜湖~~~

class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int sum = numBottles;
        while(numBottles >= numExchange){
            numBottles = numBottles - numExchange;
            numBottles++;
            sum++;
        }
        return sum;
    }
}

考虑边界问题~分子减一~芜湖 

class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        return (numBottles * numExchange -1)/ (numExchange - 1);
    }
}

python3:

class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        return (numBottles * numExchange - 1) // (numExchange - 1) 
class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        sum = numBottles
        while( numBottles >= numExchange ):
            numBottles = numBottles - numExchange
            numBottles += 1
            sum += 1
        return sum

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存