problem
solution1:
class Solution { public: int arrangeCoins(int n) { long sum = 0; int curLevel = 0; while(sum<=n) { curLevel += 1; sum += curLevel; } return (curLevel-1); } };
solution2:
class Solution { public: int arrangeCoins(int n) { return (int)((sqrt(8*(long)n+1)-1)*0.5);//数学求和公式求解x,注意参数类型是否越界. } };
solution3:
class Solution { public: int arrangeCoins(int n) { if(n<=1) return n;//err. int left = 0, right = n, mid = 0; while(left<right) { mid = left + (right-left)*0.5; if((long)mid*(mid+1)*0.5 <= n) left = mid+1; else right = mid; } return left-1;//err. } };
参考
1. Leetcode_441. Arranging Coins;
完