Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 00beebe

Browse files
committed
update.
1 parent 63d0388 commit 00beebe

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

0441-排列硬币.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 排列硬币
2+
3+
## 题目描述
4+
5+
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
6+
7+
给定一个数字 n,找出可形成完整阶梯行的总行数。
8+
9+
n 是一个非负整数,并且在32位有符号整型的范围内。
10+
11+
```
12+
n = 5
13+
硬币可排列成以下几行:
14+
x
15+
x x
16+
x x
17+
因为第三行不完整,所以返回2.
18+
19+
n = 8
20+
21+
硬币可排列成以下几行:
22+
x
23+
x x
24+
x x x
25+
x x
26+
因为第四行不完整,所以返回3.
27+
```
28+
29+
## 分析
30+
31+
已知等差数列求和公式`n = (1 + x) * x / 2` ,那么等式变换可得 `x = (-1 + sqrt(8 * n + 1)) / 2`
32+
33+
## 算法
34+
35+
```java
36+
class Solution {
37+
public int arrangeCoins(int n) {
38+
return (int)(-1 + Math.sqrt(1 + (long)n << 3)) >> 1;
39+
}
40+
}
41+
```

0 commit comments

Comments
 (0)