Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e92467e

Browse files
author
openset
committed
Add: Ugly Number II
1 parent d8bac59 commit e92467e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

‎internal/leetcode/problems_status.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ var problemStatus = map[int]bool{
8989
237: true,
9090
242: true,
9191
258: true,
92+
263: true,
93+
264: true,
9294
268: true,
9395
283: true,
9496
290: true,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
package problem264
2+
3+
func nthUglyNumber(n int) int {
4+
ans, idx := make([]int, n), [3]int{}
5+
ans[0] = 1
6+
for i := 1; i < n; i++ {
7+
for j, n := range [...]int{2, 3, 5} {
8+
if ans[idx[j]]*n <= ans[i-1] {
9+
idx[j]++
10+
}
11+
if num := ans[idx[j]] * n; j == 0 {
12+
ans[i] = num
13+
} else if ans[i] > num {
14+
ans[i] = num
15+
}
16+
}
17+
}
18+
return ans[n-1]
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package problem264
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected int
8+
}
9+
10+
func TestNthUglyNumber(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 10,
14+
expected: 12,
15+
},
16+
{
17+
input: 9,
18+
expected: 10,
19+
},
20+
{
21+
input: 60,
22+
expected: 384,
23+
},
24+
{
25+
input: 90,
26+
expected: 1152,
27+
},
28+
{
29+
input: 1,
30+
expected: 1,
31+
},
32+
}
33+
for _, tc := range tests {
34+
output := nthUglyNumber(tc.input)
35+
if output != tc.expected {
36+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
37+
}
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /