否则,第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; } }...
LeetCode 441. Arranging Coins 内容转自:http://bookshadow.com/weblog/2016/10/30/leetcode-arranging-coins/ 题目大意: 你有n枚硬币,想要组成一个阶梯形状,其中第k行放置k枚硬币。 给定n,计算可以形成的满阶梯的最大行数。 n是非负整数,并且在32位带符号整数范围之内。 解题思路: 解法I 解一元二次...
classSolution {public:intarrangeCoins(intn) {inti=0;while(true) {if(i*(i+1)/2<=n) i++;elsebreak; }returni-1; } }; 然后就想空间换时间,变成下面这样: classSolution {public:intarrangeCoins(intn) {intsize=pow(2,16);intnums[size]; nums[1]=1;for(inti=2;i<size;i++) { nums[...
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
LeetCode——441.Arranging Coins 441.Arranging Coins 问题描述 您总共有n个硬币,您想要形成阶梯形状,其中每k行必须有正好k个硬币。给定n,找到可以形成的全部楼梯行的总数。 n是非负整数,适合32位有符号整数的范围。 例子: 解法思路 ...[leetcode] 441. Arranging Coins Description You have a total of ...
代码语言: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.
// LeetCode 2020 easy #846// 441. Arranging Coins// https://leetcode.com/problems/arranging-coins/// Runtime: 0 ms, faster than 100.00% of C++ online submissions for Arranging Coins.// Memory Usage: 6.4 MB, less than 100.00% of C++ online submissions for Arranging Coins.classSolution{...
441. Arranging Coins classSolution(object):defarrangeCoins(self,n):""":type n: int:rtype: int"""# # 法一:二分法# l, r = 1, n# while l <= r:# mid = l + (r - l) // 2# _sum = mid * (1 + mid) // 2# if _sum > n: # 注意这里不取等,因为相等时r不应该变# r...