answer:
Method 1: enumeration method (not explained)
Method 2: Arithmetic Progression [Method] be appreciated that
Method 3: [dichotomy be appreciated]
Code:
class Solution {
public:
int arrangeCoins(int n) {
/* 方法1:枚举 */
if(n == 0)
{
return 0;
}
int res = 1;
long long tmp = 0;
for(int i = 2; ; i++)
{
tmp += i;
if(tmp >= n)
{
return res;
}
res++;
}
/* 方法2:等差数列公式法【待理解】
return (int) (-1 + sqrt(1 + 8 * (long) n)) / 2;
*/
/* 方法3:二分法【待理解】*/
}
};