当前位置:网站首页>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
边栏推荐
- [format] simple output (2)
- Processes and threads
- 全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
- TP5 multi conditional where query (using PHP variables)
- Creating wechat voucher process with PHP
- Basic SQL (VIII) data update operation practice
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- Laravel new route file
- Niuke white moon race 5 [problem solving mathematics field]
- Introduction to ACM [TSP problem]
猜你喜欢
ASP.NET和ASP.NETCore多环境配置对比
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Recursion - outputs continuously increasing numbers
Laravel new route file
Deep q-network (dqn)
tf. keras. layers. Timedistributed function
Encapsulate components such as pull-down menu based on ele
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Tips in MATLAB
利用栈的回溯来解决“文件的最长绝对路径”问题
随机推荐
.Net Core 限流控制-AspNetCoreRateLimit
TP5 customization in extend directory succeeded and failed. Return information
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
Er and eer models
Xamarin效果第二十二篇之录音效果
Restart redis
Basic workflow of CPU
[software testing] understand the basic knowledge of software testing
.NET7之MiniAPI(特别篇):.NET7 Preview3
c#语法糖模式匹配【switch 表达式】
AspNetCore配置多环境log4net配置文件
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Openfeign timeout setting
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
Typescript Learning Guide
AOT和单文件发布对程序性能的影响
Passing object type parameters through openfeign
How to write the expected salary on your resume to double your salary during the interview?
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
Regular object type conversion tool - Common DOM class