Skip to content

Commit f5caf41

Browse files
authored
feat: add solutions to lc problem: No.386 (doocs#3015)
No.0386.Lexicographical Numbers
1 parent 4cdb9bb commit f5caf41

12 files changed

+267
-401
lines changed

solution/0300-0399/0386.Lexicographical Numbers/README.md

+91-141
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ tags:
5151

5252
<!-- solution:start -->
5353

54-
### 方法一:DFS
54+
### 方法一:迭代
55+
56+
我们首先定义一个变量 $v$,初始时 $v = 1$。然后我们从 $1$ 开始迭代,每次迭代都将 $v$ 添加到答案数组中。然后,如果 $v \times 10 \leq n$,我们将 $v$ 更新为 $v \times 10$;否则,如果 $v \bmod 10 = 9$ 或者 $v + 1 > n$,我们就循环将 $v$ 除以 $10$。循环结束后,我们将 $v$ 加一。继续迭代,直到我们添加了 $n$ 个数到答案数组中。
57+
58+
时间复杂度 $O(n)$,其中 $n$ 是给定的整数 $n$。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
5559

5660
<!-- tabs:start -->
5761

@@ -60,16 +64,16 @@ tags:
6064
```python
6165
class Solution:
6266
def lexicalOrder(self, n: int) -> List[int]:
63-
def dfs(u):
64-
if u > n:
65-
return
66-
ans.append(u)
67-
for i in range(10):
68-
dfs(u * 10 + i)
69-
7067
ans = []
71-
for i in range(1, 10):
72-
dfs(i)
68+
v = 1
69+
for _ in range(n):
70+
ans.append(v)
71+
if v * 10 <= n:
72+
v *= 10
73+
else:
74+
while v % 10 == 9 or v + 1 > n:
75+
v //= 10
76+
v += 1
7377
return ans
7478
```
7579

@@ -78,131 +82,7 @@ class Solution:
7882
```java
7983
class Solution {
8084
public List<Integer> lexicalOrder(int n) {
81-
List<Integer> ans = new ArrayList<>();
82-
for (int i = 1; i < 10; ++i) {
83-
dfs(i, n, ans);
84-
}
85-
return ans;
86-
}
87-
88-
private void dfs(int u, int n, List<Integer> ans) {
89-
if (u > n) {
90-
return;
91-
}
92-
ans.add(u);
93-
for (int i = 0; i < 10; ++i) {
94-
dfs(u * 10 + i, n, ans);
95-
}
96-
}
97-
}
98-
```
99-
100-
#### C++
101-
102-
```cpp
103-
class Solution {
104-
public:
105-
vector<int> lexicalOrder(int n) {
106-
vector<int> ans;
107-
for (int i = 1; i < 10; ++i) dfs(i, n, ans);
108-
return ans;
109-
}
110-
111-
void dfs(int u, int n, vector<int>& ans) {
112-
if (u > n) return;
113-
ans.push_back(u);
114-
for (int i = 0; i < 10; ++i) dfs(u * 10 + i, n, ans);
115-
}
116-
};
117-
```
118-
119-
#### Go
120-
121-
```go
122-
func lexicalOrder(n int) []int {
123-
var ans []int
124-
var dfs func(u int)
125-
dfs = func(u int) {
126-
if u > n {
127-
return
128-
}
129-
ans = append(ans, u)
130-
for i := 0; i < 10; i++ {
131-
dfs(u*10 + i)
132-
}
133-
}
134-
for i := 1; i < 10; i++ {
135-
dfs(i)
136-
}
137-
return ans
138-
}
139-
```
140-
141-
#### Rust
142-
143-
```rust
144-
impl Solution {
145-
fn dfs(mut num: i32, n: i32, res: &mut Vec<i32>) {
146-
if num > n {
147-
return;
148-
}
149-
res.push(num);
150-
for i in 0..10 {
151-
Self::dfs(num * 10 + i, n, res);
152-
}
153-
}
154-
155-
pub fn lexical_order(n: i32) -> Vec<i32> {
156-
let mut res = vec![];
157-
for i in 1..10 {
158-
Self::dfs(i, n, &mut res);
159-
}
160-
res
161-
}
162-
}
163-
```
164-
165-
#### JavaScript
166-
167-
```js
168-
/**
169-
* @param {number} n
170-
* @return {number[]}
171-
*/
172-
var lexicalOrder = function (n) {
173-
let ans = [];
174-
function dfs(u) {
175-
if (u > n) {
176-
return;
177-
}
178-
ans.push(u);
179-
for (let i = 0; i < 10; ++i) {
180-
dfs(u * 10 + i);
181-
}
182-
}
183-
for (let i = 1; i < 10; ++i) {
184-
dfs(i);
185-
}
186-
return ans;
187-
};
188-
```
189-
190-
<!-- tabs:end -->
191-
192-
<!-- solution:end -->
193-
194-
<!-- solution:start -->
195-
196-
### 方法二
197-
198-
<!-- tabs:start -->
199-
200-
#### Java
201-
202-
```java
203-
class Solution {
204-
public List<Integer> lexicalOrder(int n) {
205-
List<Integer> ans = new ArrayList<>();
85+
List<Integer> ans = new ArrayList<>(n);
20686
int v = 1;
20787
for (int i = 0; i < n; ++i) {
20888
ans.add(v);
@@ -230,10 +110,12 @@ public:
230110
int v = 1;
231111
for (int i = 0; i < n; ++i) {
232112
ans.push_back(v);
233-
if (v * 10 <= n)
113+
if (v * 10 <= n) {
234114
v *= 10;
235-
else {
236-
while (v % 10 == 9 || v + 1 > n) v /= 10;
115+
} else {
116+
while (v % 10 == 9 || v + 1 > n) {
117+
v /= 10;
118+
}
237119
++v;
238120
}
239121
}
@@ -245,8 +127,7 @@ public:
245127
#### Go
246128
247129
```go
248-
func lexicalOrder(n int) []int {
249-
var ans []int
130+
func lexicalOrder(n int) (ans []int) {
250131
v := 1
251132
for i := 0; i < n; i++ {
252133
ans = append(ans, v)
@@ -259,10 +140,79 @@ func lexicalOrder(n int) []int {
259140
v++
260141
}
261142
}
262-
return ans
143+
return
144+
}
145+
```
146+
147+
#### TypeScript
148+
149+
```ts
150+
function lexicalOrder(n: number): number[] {
151+
const ans: number[] = [];
152+
let v = 1;
153+
for (let i = 0; i < n; ++i) {
154+
ans.push(v);
155+
if (v * 10 <= n) {
156+
v *= 10;
157+
} else {
158+
while (v % 10 === 9 || v === n) {
159+
v = Math.floor(v / 10);
160+
}
161+
++v;
162+
}
163+
}
164+
return ans;
263165
}
264166
```
265167

168+
#### Rust
169+
170+
```rust
171+
impl Solution {
172+
pub fn lexical_order(n: i32) -> Vec<i32> {
173+
let mut ans = Vec::with_capacity(n as usize);
174+
let mut v = 1;
175+
for _ in 0..n {
176+
ans.push(v);
177+
if v * 10 <= n {
178+
v *= 10;
179+
} else {
180+
while v % 10 == 9 || v + 1 > n {
181+
v /= 10;
182+
}
183+
v += 1;
184+
}
185+
}
186+
ans
187+
}
188+
}
189+
```
190+
191+
#### JavaScript
192+
193+
```js
194+
/**
195+
* @param {number} n
196+
* @return {number[]}
197+
*/
198+
var lexicalOrder = function (n) {
199+
const ans = [];
200+
let v = 1;
201+
for (let i = 0; i < n; ++i) {
202+
ans.push(v);
203+
if (v * 10 <= n) {
204+
v *= 10;
205+
} else {
206+
while (v % 10 === 9 || v === n) {
207+
v = Math.floor(v / 10);
208+
}
209+
++v;
210+
}
211+
}
212+
return ans;
213+
};
214+
```
215+
266216
<!-- tabs:end -->
267217

268218
<!-- solution:end -->

0 commit comments

Comments
 (0)