当前位置:网站首页>386字典序排数,学会用递归的思想
386字典序排数,学会用递归的思想
2022-04-21 07:15:00 【糊涂不是傻】
刚拿到这个问题的时候,我想到的只是第一位字典序排序,所以我把第一位为1的数字按照从小到大的顺序排下去了,很明显这样是有问题的,下面举个例子:
1,10,11.。。。100,但是100应该是在10后面的,按照字典序的原则,0一样,这样的话单纯的迭代可能就解决不了这个问题了,因为每次在结果列表里面放入一个数之后,应该考虑比它位数多的数字里面有没有前几位和它一样的数。
想到这里我也想到了用链表来求解这个问题,因为链表在插入的时候只需要O(n)的复杂度,这个问题跟桶排序有点类似,但又有点不同,因为它不是排序,而是排数。
所以这题选用递归,因为递归这东西好用,比如我现在从10跳到了100,在100这里的递归结束之后还能再回到10,这就节省了后续搜索插入的时间。
class Solution:
def lexicalOrder(self, n: int) -> List[int]:
count = 0
result = []
def function(x,n):
if x<=n:
result.append(x)
for i in range(x*10,x*10+10):
function(i,n) #递归
while(int(n/math.pow(10,count))>0):
count+=1
for j in range(1,10):
function(j,n)
return result
版权声明
本文为[糊涂不是傻]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43872912/article/details/124305641
边栏推荐
- Access-Control-Allow-Credentials:true 和 预检请求
- 上古神器Vim
- 【项目】小帽外卖(六)
- Codeforces Round #783 (Div. 2) ABC
- Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by serv
- 【Web系统课程设计】2022版
- You cannot set a form field before rendering a field associated with the value
- EF de duplication operation
- Project storage log
- 联合类型和类型保护
猜你喜欢

Access-Control-Allow-Credentials:true 和 预检请求

Valentina Studio Pro for Mac(mac数据库管理软件)

Acrobat Pro DC 教程::如何使用文本和图片文件创建 PDF?
![[2022dasctf x Su] Web replay of March spring challenge](/img/75/cd6d8d54cd0435e0b02110b4061ca7.png)
[2022dasctf x Su] Web replay of March spring challenge

Pycharm's latest method of importing PIL Library

VMware提示恢复快照时出错找不到所需文件

如何修改 Rancher Server 的 IP 地址

Apache solr 远程代码执行漏洞(CVE-2019-0193)复现

【以太网交换安全】--- 端口隔离运行原理及二层隔离三层通信实例配置讲解

2022年危险化学品生产单位安全生产管理人员考试模拟100题模拟考试平台操作
随机推荐
. net core throws an error and writes it Txt file
TIANTI race L3
The listview column in C automatically adapts to the perfect effect of scaling
Virtual machine host Ping SSH campus network bridge net
Vim插件管理插件Vim-plug
DeprecationWarning: NewId() is deprecated
The interface is not restored after Fiddler changes the font
ECS uses FRP to map Apache on the local (win10 / win11) intranet to the extranet
剑指offer day22 位运算(中等)
【知行】浅谈Switch与If
[Ethernet switching security] - explanation of port isolation operation principle and two-layer isolation and three-layer communication example configuration
Operation of simulation examination platform of 100 simulated questions for safety production management personnel of hazardous chemical production units in 2022
Usage of go ini
信息学奥赛一本通 1209:分数求和 | OpenJudge 1.13 12:分数求和
Sword finger offer day22 bit operation (medium)
联合类型和类型保护
Batch replacement of some data of a field in MySQL
Detailed explanation of burpsuite tools and examples of Library explosion
matlab画散点图
Unity performance optimization UI