当前位置:网站首页>golang力扣leetcode 386.字典序排数
golang力扣leetcode 386.字典序排数
2022-04-21 23:07:00 【cheems~】
386.字典序排数
题解
字典序,简单题,直接看注释即可
代码
/* 字典序 n=13 1 10 11 12 13 2 3 4 5 6 7 8 9 */
//迭代
func lexicalOrder1(n int) []int {
ans := make([]int, n)
num := 1
for i := range ans {
ans[i] = num
if num*10 <= n {
//如果num*10 <= n,说明num*10是下一个字典序
num *= 10
} else {
//如果num*10 > n,说明num*10不满足字典序,尝试num++,或者缩小
for num+1 > n {
//如果num+1>n,说明要缩小,返回上一位
num /= 10
}
for num%10 == 9 {
//如果num是 9 结尾,说明搜索到末尾,返回上一位
num /= 10
}
num++ //两种情况缩小后,加一
}
}
return ans
}
//递归
func lexicalOrder2(n int) []int {
//因为dfs是闭包捕获了ans切片,所以不用传指针,闭包内外用的是同一个ans
ans := make([]int, 0, n)
var dfs func(int, int)
dfs = func(cur int, limit int) {
//如果超过限制则返回
if cur > limit {
return
} else {
//在范围内则添加,因为是dfs,那么是天然满足字典序的
ans = append(ans, cur)
//递归放大十倍的下一个字典序
for i := 0; i <= 9; i++ {
dfs(cur*10+i, limit)
}
}
}
//入口
for i := 1; i <= 9; i++ {
dfs(i, n)
}
return ans
}
版权声明
本文为[cheems~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42956653/article/details/124320558
边栏推荐
- 【matlab中一些小技巧和快捷鍵使用總結】
- Rvb2601 startup process
- VOS如何设置盲区
- [Central South University of forestry science and technology] [Chen] week 7 innovation maze
- Why: uncaught referenceerror: effect is not defined
- Kubernetes - Secret configuration management
- OS Experiment 3 [process communication]
- Basic concepts of audio and video and a simple introduction to ffmpeg
- Industry analysis | development of Internet medicine
- Kubernetes---ConfigMap配置管理
猜你喜欢

小程序如何集成即构IM实现即时通讯发消息聊天

Tensorflow 2.8 installation

1957年高考数学题

MySQL multi table query exercise
![OS Experiment 3 [process communication]](/img/78/e89e0819ae345cb464613935508eac.png)
OS Experiment 3 [process communication]

Day code 300 lines learning notes day 46

Dynamic programming: complete knapsack problem

AI application theory - special production and manufacturing session (parts installation and quality inspection)

Introduction to cyclone IV e series
技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关
随机推荐
4. MySQL workbench create access user
P1027 [NOIP2001 提高组] Car 的旅行路线 (图 最短路)
GO111MODULE=on go mod init错误(go: cannot determine module path for source directory)
1957年高考数学题
OS Experiment 3 [process communication]
Basic concepts of audio and video and a simple introduction to ffmpeg
【FPGA的基础学习-------OV7725摄像头模块】
MySQL problem solving_ Multi table joint query_ The operator wants to calculate the average answer volume of users from different schools and different difficulties who participated in the answer. Ple
One plus two earphone products: charge for 10 minutes and listen to music for 20 hours
8.3 create a mobile robot by hand in rodf robot modeling
[Résumé de quelques conseils et raccourcis clavier dans MATLAB]
Nacos Registry - service registration and tiered storage
Discussion on digital business of traditional enterprises - Digital Architecture Design (5)
CDGA敏捷开发的12个原则在企业数据治理中的应用
VOS6. 0 installation and source code command
APM industry awareness series - 12 - 13
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
Languages supported by markdown code blocks
Some cold thoughts behind the popularity of microservices, middle office, RPA and low code
图像采集卡的概念及作用原理