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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)