原题目:https://leetcode-cn.com/problems/arranging-coins/
思路:
二分查找、迭代、求根公式
代码:
class Solution { public: int arrangeCoins(int n) { int l = 0,r=n; while(l<=r){ long mid = (r-l)/2+l; long num = mid*(mid+1)/2; if(n==num) return mid; else if(n>num) l = mid+1; else r = mid-1; } return l-1; } }; class Solution { public: int arrangeCoins(int n) { int c = 0; while(n>=0){ c += 1; n -= c; } return c-1; } }; class Solution { public: int arrangeCoins(int n) { return (-1 + sqrt(1+8*(long)n))/2; } };