当前位置:网站首页>Use DFS to solve the problem of "number of dictionary rows"
Use DFS to solve the problem of "number of dictionary rows"
2022-04-23 03:05:00 【& Xiaobai &】
sixteen 、 Dictionary order number
16.1、 Question design requirements
Give you an integer n , Returns the range in dictionary order [1, n] All integers in .
You have to design a time complexity of O(n) And the use of O(1) Extra space algorithm .
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]
Tips :
1 <= n <= 5 * 10^4
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/lexicographical-numbers
16.2、 Their thinking
First give cur take 1 To 9 The number above , And then in turn n Compare , If n Greater than or equal to cur Represents the number of , Will cur Add to res in , I'll use cur For ten , The number of hundreds is the same as n Compare , If less than or equal to n, Then continue to add res in , Finally, output .
16.3、 Algorithm
class Solution {
List<Integer> res;
public List<Integer> lexicalOrder(int n) {
res = new ArrayList<>();
for (int i = 1; i <= 9; i++) {
dfs(i , n);
}
return res;
}
//cur Current bit
public void dfs(int cur,int n){
// disqualification
if (cur > n){
return;
}
// Add eligible to res in
res.add(cur);
// Will be with cur For ten , Compare and add numbers such as hundreds
for (int i = 0; i <= 9; i++) {
int nextNum = cur * 10 + i;
if (nextNum > n){
break;
}
// If nextNum Less than n, Do it again dfs
dfs(nextNum,n);
}
}
}
Reference video :B standing up Lord Guo wg
版权声明
本文为[& Xiaobai &]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301476689.html
边栏推荐
- 腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
- How to write the expected salary on your resume to double your salary during the interview?
- 使用DFS来解决“字典序排数”问题
- Creating wechat voucher process with PHP
- 使用split来解决“最常见的单词”问题
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
- Deep q-network (dqn)
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- How to deploy a website with only a server and no domain name?
猜你喜欢

HLS / chisel practice CORDIC high performance computing complex square root

利用栈的回溯来解决“文件的最长绝对路径”问题

Tips in MATLAB

Sonic cloud real machine tutorial

Response processing of openfeign

Opencv combines multiple pictures into video

tf. keras. layers. Embedding function

Detailed log display of openfeign call

Encapsulation of ele table

AspNetCore配置多环境log4net配置文件
随机推荐
REINFORCE
Opencv combines multiple pictures into video
[learn junit5 from official documents] [II] [writingtests] [learning notes]
使用DFS来解决“字典序排数”问题
Traversée de l'arbre L2 - 006
一套关于 内存对齐 的C#面试题,做错的人很多!
eventBus
TP5 where query one field is not equal to multiple values
Laravel8- use JWT
Basic workflow of CPU
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
Use of MySQL command line client and common commands
[software testing] understand the basic knowledge of software testing
微软是如何解决 PC 端程序多开问题的——内部实现
The space between the left and right of the movie ticket seats is empty and cannot be selected
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
[Euler plan question 13] sum of large numbers
最通俗易懂的依赖注入与控制反转
Cherno_ Game engine series tutorial (5): 101~
ASP.NET和ASP.NETCore多环境配置对比