988. 硬币摆放

988. 硬币摆放

中文English

你有 n 枚硬币,想要摆放成阶梯形状,即第 k 行恰好有 k 枚硬币。

给出 n,找到可以形成的完整楼梯行数。

n 是一个非负整数,且在32位有符号整数范围内。

样例

样例 1:

输入:n = 5
输出:2
解释:
硬币可以形成以下行:
¤
¤ ¤
¤ ¤
因为第3行不完整,我们返回2。

样例 2:

输入:n = 8
输出:3
解释:
硬币可以形成以下行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
因为第4行不完整,我们返回3。
 
 
输入测试数据 (每行一个参数)如何理解测试数据?
class Solution:
    """
    @param n: a non-negative integer
    @return: the total number of full staircase rows that can be formed
    """
    '''
    大致思路:
    1.首先阶梯行数是按照1,2,3,4...的顺序进行排放的,那么可以初始化s = 0while true,s = s + i,直到s大于n
    的时候,返回s - n,即可.
    '''
    def arrangeCoins(self,n):
        s = 0
        i = 0
        while True:
            if s > n:
                return i - 1
            i += 1
            s += i
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12592301.html