当前位置:网站首页>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
边栏推荐
- TP5 where query one field is not equal to multiple values
- Creating wechat voucher process with PHP
- 使用DFS来解决“字典序排数”问题
- 求二叉树的叶子结点个数
- 一套关于 内存对齐 的C#面试题,做错的人很多!
- [Euler plan question 13] sum of large numbers
- Basic SQL (VIII) data update operation practice
- Systemctl start Prometheus + grafana environment
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
猜你喜欢

Openfeign details show

Tips in MATLAB

Guangcheng cloud service can fill in a daily report regularly every day

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

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)

Response processing of openfeign

Service avalanche effect

TP5 inherits base and uses the variables in base

腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元

Q-Learning & Sarsa
随机推荐
Systemctl start Prometheus + grafana environment
Load view Caton
HLS / chisel practice CORDIC high performance computing complex square root
Service avalanche effect
TP5 customization in extend directory succeeded and failed. Return information
Basic SQL (VIII) data update operation practice
Numpy append function
JSON data text
REINFORCE
使用split来解决“最常见的单词”问题
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
Openfeign service call
Introduction to ACM [TSP problem]
tf. keras. layers. Inputlayer function
宁德时代地位不保?
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
tf. keras. layers. Timedistributed function
[format] simple output (2)
Chapter VI project information management system summary