Skip to content

Commit 4cd13d9

Browse files
committed
feat: add solutions to lcof question: 49.Ugly Number
添加《剑指 Offer》题解:面试题49. 丑数
1 parent d72cbc2 commit 4cd13d9

File tree

6 files changed

+123
-36
lines changed

6 files changed

+123
-36
lines changed

.gitignore

-32
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,3 @@
1-
/gradle/wrapper/gradle-wrapper.properties
2-
##----------Android----------
3-
# build
4-
*.apk
5-
*.ap_
6-
*.dex
7-
*.class
8-
bin/
9-
gen/
10-
build/
11-
12-
# gradle
13-
.gradle/
14-
gradle-app.setting
15-
!gradle-wrapper.jar
16-
build/
17-
18-
local.properties
19-
20-
##----------idea----------
21-
*.iml
221
.idea/
23-
*.ipr
24-
*.iws
25-
26-
# Android Studio Navigation editor temp files
27-
.navigation/
28-
29-
##----------Other----------
30-
# osx
31-
*~
322
.DS_Store
33-
gradle.properties
34-
353
.vscode

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
99
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
1010
<a href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
11-
<!-- <a href="https://github.com/doocs/leetcode" alt="github"><img src="https://badgen.net/badge/⭐/GitHub/cyan" /></a>
12-
<a href="https://gitee.com/doocs/leetcode" alt="gitee"><img src="https://badgen.net/badge/⭐/Gitee/cyan" /></a> -->
1311
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
1412
<a href="https://doocs.github.io/#/?id=how-to-join"><img src="https://badgen.net/badge/organization/join%20us/cyan" alt="open-source-organization"></a>
1513
</p>

README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
99
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
1010
<a href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
11-
<!-- <a href="https://github.com/doocs/leetcode" alt="github"><img src="https://badgen.net/badge/⭐/GitHub/cyan" /></a>
12-
<a href="https://gitee.com/doocs/leetcode" alt="gitee"><img src="https://badgen.net/badge/⭐/Gitee/cyan" /></a> -->
1311
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
1412
<a href="https://doocs.github.io/#/?id=how-to-join"><img src="https://badgen.net/badge/organization/join%20us/cyan" alt="open-source-organization"></a>
1513
</p>

lcof/面试题49. 丑数/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [面试题49. 丑数](https://leetcode-cn.com/problems/chou-shu-lcof/)
2+
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
我们把只包含因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。
6+
7+
**示例:**
8+
9+
```
10+
输入: n = 10
11+
输出: 12
12+
解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。
13+
```
14+
15+
**说明:**
16+
17+
1. `1` 是丑数。
18+
2. `n` 不超过 1690。
19+
20+
21+
## 解法
22+
<!-- 这里可写通用的实现逻辑 -->
23+
24+
25+
### Python3
26+
<!-- 这里可写当前语言的特殊实现逻辑 -->
27+
28+
```python
29+
class Solution:
30+
def nthUglyNumber(self, n: int) -> int:
31+
if n < 7:
32+
return n
33+
dp = [1 for _ in range(n)]
34+
i2 = i3 = i5 = 0
35+
for i in range(1, n):
36+
next2, next3, next5 = dp[i2] * 2, dp[i3] * 3, dp[i5] * 5
37+
dp[i] = min(next2, next3, next5)
38+
if dp[i] == next2:
39+
i2 += 1
40+
if dp[i] == next3:
41+
i3 += 1
42+
if dp[i] == next5:
43+
i5 += 1
44+
return dp[n - 1]
45+
46+
```
47+
48+
### Java
49+
<!-- 这里可写当前语言的特殊实现逻辑 -->
50+
51+
```java
52+
class Solution {
53+
public int nthUglyNumber(int n) {
54+
if (n < 7) {
55+
return n;
56+
}
57+
int[] dp = new int[n];
58+
dp[0] = 1;
59+
int i2 = 0, i3 = 0, i5= 0;
60+
for (int i = 1; i < n; ++i) {
61+
int next2 = dp[i2] * 2, next3 = dp[i3] * 3, next5 = dp[i5] * 5;
62+
dp[i] = Math.min(Math.min(next2, next3), next5);
63+
if (dp[i] == next2) {
64+
++i2;
65+
}
66+
if (dp[i] == next3) {
67+
++i3;
68+
}
69+
if (dp[i] == next5) {
70+
++i5;
71+
}
72+
}
73+
return dp[n - 1];
74+
75+
}
76+
}
77+
```
78+
79+
### ...
80+
```
81+
82+
```
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int nthUglyNumber(int n) {
3+
if (n < 7) {
4+
return n;
5+
}
6+
int[] dp = new int[n];
7+
dp[0] = 1;
8+
int i2 = 0, i3 = 0, i5 = 0;
9+
for (int i = 1; i < n; ++i) {
10+
int next2 = dp[i2] * 2, next3 = dp[i3] * 3, next5 = dp[i5] * 5;
11+
dp[i] = Math.min(Math.min(next2, next3), next5);
12+
if (dp[i] == next2) {
13+
++i2;
14+
}
15+
if (dp[i] == next3) {
16+
++i3;
17+
}
18+
if (dp[i] == next5) {
19+
++i5;
20+
}
21+
}
22+
return dp[n - 1];
23+
24+
}
25+
}

lcof/面试题49. 丑数/Solution.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def nthUglyNumber(self, n: int) -> int:
3+
if n < 7:
4+
return n
5+
dp = [1 for _ in range(n)]
6+
i2 = i3 = i5 = 0
7+
for i in range(1, n):
8+
next2, next3, next5 = dp[i2] * 2, dp[i3] * 3, dp[i5] * 5
9+
dp[i] = min(next2, next3, next5)
10+
if dp[i] == next2:
11+
i2 += 1
12+
if dp[i] == next3:
13+
i3 += 1
14+
if dp[i] == next5:
15+
i5 += 1
16+
return dp[n - 1]

0 commit comments

Comments
 (0)