class Solution(object): def arrangeCoins(self, n): k = 0 while k*(k+1)/2 <= n: k += 1 return k-1
二分枚举答案(Binary Search)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Solution(object): def arrangeCoins(self, n): """ :type n: int :rtype: int """ l, r = 0, n while l <= r: m = (l + r) / 2 if m * (m + 1) / 2 > n: r = m - 1 else: l = m + 1 return r
class Solution(object): def arrangeCoins(self, n): """ :type n: int :rtype: int """ total = 0 row = 1 if n == 0: return 0 if n == 1: return 1 while True: if n - total < row: return row - 1 elif n - total == row: return row else: total += row row += 1