?LeetCode刷題實(shí)戰(zhàn)441:排列硬幣
You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.
Given the integer n, return the number of complete rows of the staircase you will build.
示例


?
解題
class?Solution?{
public:
????int?arrangeCoins(int?n)?{
????????int?l = 1, r = n;
????????if?(n == 0)
????????????return?0;
????????while?(l < r)
????????{
????????????long?long?mid = l + (r - l + 1) / 2;
????????????if?((1?+ mid) * mid / 2?<= n)
????????????????l = mid;
????????????else
????????????????r = mid - 1;
????????}
????????return?l ;
????}
};
LeetCode1-440題匯總,希望對(duì)你有點(diǎn)幫助!
評(píng)論
圖片
表情
