-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path386-LexicographicalNumbers.go
More file actions
140 lines (123 loc) · 3.39 KB
/
386-LexicographicalNumbers.go
File metadata and controls
140 lines (123 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
// 386. Lexicographical Numbers
// Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.
// You must write an algorithm that runs in O(n) time and uses O(1) extra space.
// Example 1:
// Input: n = 13
// Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
// Example 2:
// Input: n = 2
// Output: [1,2]
// Constraints:
// 1 <= n <= 5 * 10^4
import "fmt"
import "container/list"
// stack
func lexicalOrder(n int) []int {
res := []int{}
for i := 1; len(res) < n && i <= 9; i++ {
stack := list.New()
stack.PushBack(i)
for stack.Len() > 0 {
current := stack.Remove(stack.Back()).(int)
if current <= n {
res = append(res, current)
for j := 9; j >= 0; j-- {
stack.PushBack(current * 10 + j)
}
}
}
}
return res
}
// dfs
func lexicalOrder1(n int) []int {
res := make([]int, 0, n)
var dfs func(x, n int, res *[]int)
dfs = func(x, n int, res *[]int) {
limit := (x + 10) / 10 * 10
for x <= n && x < limit {
*res = append(*res, x)
if x*10 <= n {
dfs(x*10, n, res)
}
x++
}
}
dfs(1, n, &res)
return res
}
func lexicalOrder2(n int) []int {
res := []int{}
var dfs func(cur, limit int)
dfs = func(cur, limit int) {
if cur > limit { return; }
res = append(res, cur)
for i := 0; i <= 9; i++ {
if cur * 10 + i > limit {
return
}
dfs(cur*10 + i, limit)
}
}
for i := 1; i <= 9; i++ {
dfs(i, n)
}
return res
}
func lexicalOrder3(n int) []int {
res, num := make([]int, n), 1
for i := 0; i < len(res); i++ {
res[i] = num
if num * 10 <= n {
num = num * 10
} else {
for num % 10 == 9 || num + 1 > n {
num = num / 10
}
num++
}
}
return res
}
func lexicalOrder4(n int) []int {
res, index := make([]int, n), 0
stack := []int{1}
res[0] = 1
for len(stack) > 0 {
v := stack[len(stack) - 1]
stack = stack[:len(stack) - 1] // pop
if v > n { continue }
res[index] = v
index++
if v % 10 != 9 {
stack = append(stack, v + 1)
}
stack = append(stack, v * 10)
}
return res
}
func main() {
// Example 1:
// Input: n = 13
// Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
fmt.Println(lexicalOrder(13)) // [1,10,11,12,13,2,3,4,5,6,7,8,9]
// Example 2:
// Input: n = 2
// Output: [1,2]
fmt.Println(lexicalOrder(2)) // [1,2]
fmt.Println(lexicalOrder(1)) // [1]
//fmt.Println(lexicalOrder(50000)) // [1,2]
fmt.Println(lexicalOrder1(13)) // [1,10,11,12,13,2,3,4,5,6,7,8,9]
fmt.Println(lexicalOrder1(2)) // [1,2]
fmt.Println(lexicalOrder1(1)) // [1]
fmt.Println(lexicalOrder2(13)) // [1,10,11,12,13,2,3,4,5,6,7,8,9]
fmt.Println(lexicalOrder2(2)) // [1,2]
fmt.Println(lexicalOrder2(1)) // [1]
fmt.Println(lexicalOrder3(13)) // [1,10,11,12,13,2,3,4,5,6,7,8,9]
fmt.Println(lexicalOrder3(2)) // [1,2]
fmt.Println(lexicalOrder3(1)) // [1]
fmt.Println(lexicalOrder4(13)) // [1,10,11,12,13,2,3,4,5,6,7,8,9]
fmt.Println(lexicalOrder4(2)) // [1,2]
fmt.Println(lexicalOrder4(1)) // [1]
}