給定一個n尋找可以填滿的樓梯階層數。
因為階層如下圖: o oo ooo oooo … 所以將數字從0開始累加即可知道總共有幾層為填滿的,這邊應該會有數學解,但先以簡單好懂為主。
程式碼:
def arrangeCoins(self, n: int) -> int: ans = 0 if(ans>=n): return ans for i in range(n+1): ans+=i if(ans==n): return i elif(ans>n): return i-1
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up