8000 ugly_number_II · hitzzc/go-leetcode@7c44962 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c44962

Browse files
committed
ugly_number_II
1 parent 476a880 commit 7c44962

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
202202
#### [258. Add Digits](https://github.com/hitzzc/go-leetcode/tree/master/add_digits)
203203
#### [260. Single Number III](https://github.com/hitzzc/go-leetcode/tree/master/single_number_III)
204204
#### [263. Ugly Number](https://github.com/hitzzc/go-leetcode/tree/master/ugly_number)
205+
#### [264. Ugly Number II](https://github.com/hitzzc/go-leetcode/tree/master/ugly_number_II)
205206

206207

207208

ugly_number_II/ugly_number_II.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ugly_number_II
2+
3+
func nthUglyNumber(n int) int {
4+
if n == 1 {
5+
return 1
6+
}
7+
records := []int{1}
8+
i, j, k := 0, 0, 0
9+
for len(records) < n {
10+
next := min(2*records[i], min(3*records[j], 5*records[k]))
11+
if next == 2*records[i] {
12+
i++
13+
}
14+
if next == 3*records[j] {
15+
j++
16+
}
17+
if next == 5*records[k] {
18+
k++
19+
}
20+
records = append(records, next)
21+
}
22+
return records[len(records)-1]
23+
}
24+
25+
func min(i, j int) int {
26+
if i < j {
27+
return i
28+
}
29+
return j
30+
}

0 commit comments

Comments
 (0)
0