否则,第mid行可能够也可能小于,所以我们保留,并取右边,L = mid classSolution {publicintarrangeCoins(intn) {intL = 0;intR =n;while(L<R){intmid = (L+R+1)>>>1;if((long)(Math.pow(mid, 2)+mid)>(long)2*n){ R= mid-1; }else{ L=mid; } }returnL; } }...
Arranging Coins问题在Leetcode上的难度等级是多少? Arranging Coins 标准答案代码来自书影博客 题目大意 你有n枚硬币,想要组成一个阶梯形状,其中第k行放置k枚硬币。 给定n,计算可以形成的满阶梯的最大行数。 n是非负整数,并且在32位带符号整数范围之内。 解题思路 数学方法 每行想填满需要k*(k+1)/2个硬币 二...
0.5*m+0.5*m*m <=n 表示m是合适的,或者偏小了,这个时候增大l,如果l>r,r就一定落在m处 1publicclassSolution {2publicintarrangeCoins(intn) {3intl=1, r=n;4while(l <=r) {5intm = l + (r-l)/2;6if(0.5*m+0.5*m*m >n) {7r = m - 1;8}9elsel = m + 1;10}11returnr;12}...
solution2: AI检测代码解析 class Solution { public: int arrangeCoins(int n) { return (int)((sqrt(8*(long)n+1)-1)*0.5);//数学求和公式求解x,注意参数类型是否越界. } }; 1. 2. 3. 4. 5. 6. solution3: AI检测代码解析 class Solution { public: int arrangeCoins(int n) { if(n<=1...
Can you solve this real interview question? Arranging Coins - 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. G
441. Arranging Coins aliblielite 来自专栏 · leetcode_python_easyclass Solution(object): def arrangeCoins(self, n): """ :type n: int :rtype: int """ # # 法一:二分法 # l, r = 1, n # while l <= r: # mid = l + (r - l) // 2 #...
代码语言:javascript 代码运行次数:0 运行 AI代码解释 publicintarrangeCoins(int n){long rgt=(int)(Math.sqrt(n)*Math.sqrt(2)+1);long lft=0;while(lft<=rgt){long mid=lft+(rgt-lft)/2;long total=(mid+1)*mid/2;if(total<=n){lft=mid+1;}else{rgt=mid-1;}}return(int)lft-1;}...
https://leetcode.com/problems/arranging-coins/ 题目描述 You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full stair...
class Solution: def arrangeCoins(self, n): """ :type n: int :rtype: int """ return int((math.sqrt(n*2+0.25)-0.5)) 1 2 3 4 5 6 7While the code is focused, press Alt+F1 for a menu of operations.
441. 排列硬币 - 你总共有 n 枚硬币,并计划将它们按阶梯状排列。对于一个由 k 行组成的阶梯,其第 i 行必须正好有 i 枚硬币。阶梯的最后一行 可能 是不完整的。 给你一个数字 n ,计算并返回可形成 完整阶梯行 的总行数。 示例 1: [https://assets.leetcode.com/upl