mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 386
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package leetcode
|
||||
|
||||
func lexicalOrder(n int) []int {
|
||||
res := make([]int, 0, n)
|
||||
dfs386(1, n, &res)
|
||||
return res
|
||||
}
|
||||
|
||||
func dfs386(x, n int, res *[]int) {
|
||||
limit := (x + 10) / 10 * 10
|
||||
for x <= n && x < limit {
|
||||
*res = append(*res, x)
|
||||
if x*10 <= n {
|
||||
dfs386(x*10, n, res)
|
||||
}
|
||||
x++
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question386 struct {
|
||||
para386
|
||||
ans386
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para386 struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans386 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem386(t *testing.T) {
|
||||
|
||||
qs := []question386{
|
||||
|
||||
question386{
|
||||
para386{13},
|
||||
ans386{[]int{1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 386------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans386, q.para386
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, lexicalOrder(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
25
Algorithms/0386. Lexicographical Numbers/README.md
Executable file
25
Algorithms/0386. Lexicographical Numbers/README.md
Executable file
@@ -0,0 +1,25 @@
|
||||
# [386. Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)
|
||||
|
||||
|
||||
## 题目:
|
||||
|
||||
Given an integer n, return 1 - n in lexicographical order.
|
||||
|
||||
For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].
|
||||
|
||||
Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个整数 n, 返回从 1 到 n 的字典顺序。例如,给定 n =13,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 。
|
||||
|
||||
请尽可能的优化算法的时间复杂度和空间复杂度。 输入的数据 n 小于等于 5,000,000。
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 给出一个数字 n ,要求按照字典序对 1-n 这 n 个数排序。
|
||||
- DFS 暴力求解即可。
|
||||
Reference in New Issue
Block a user