class Solution {
public:
int arrangeCoins(int n) {
/*
int row=0,sum=0;
while(sum+row+1<=n){
row++;
sum+=row;
}
return row;
*/ //超时
return (int)(-0.5+sqrt((long)2*n+0.25));//如果不用long,会溢出,也可以用double
}
};
441. Arranging Coins
最新推荐文章于 2024-10-13 00:59:04 发布