当前位置:网站首页>剑指offer17---打印从1到最大的n位数
剑指offer17---打印从1到最大的n位数
2022-07-31 00:55:00 【星光技术人】
打印从1到最大的n位数

题目考点:原题设定数组再INT32的范围内,所以可以直接使用for循环依次求出;但是如果数值超过INT32,就需要使用long型;如果数字比long更大怎么办;无论是 short / int / long … 任意变量类型,数字的取值范围都是有限的。因此,大数的表示应用字符串 String 类型

- 递归求解1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
string path = "";
vector<string> res;
vector<char> strs = {
'0','1','2','3','4','5','6','7','8','9'};
void printNums(int n)
{
dfs(0, n);
for (auto str : res)
cout << str << endl;
return;
}
void dfs(int idx, int n)
{
if (path.size() == n)
{
res.push_back(path);
return;
}
for (int i =0; i <= 9; i++)
{
path.push_back(strs[i]);
dfs(i, n);
path.pop_back();
}
}
};
int main()
{
Solution S;
S.printNums(2);
return 0;
}
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
- 递归解法2
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
string path = "";
vector<string> res;
vector<char> strs = {
'0','1','2','3','4','5','6','7','8','9'};
void printNums(int n)
{
res = {
"1","2","3","4","5","6","7","8","9" };
dfs(0, n);
for (auto str : res)
cout << str << endl;
return;
}
//添加第idx个数字,目标长度为n
void dfs(int idx, int n)
{
if (idx==n)
{
res.push_back(path);
return;
}
//第一个数字不能为0
int start = idx == 0 ? 1 : 0;
for (int i =start; i <= 9; i++)
{
path.push_back(strs[i]);
dfs(idx + 1, n);
path.pop_back();
}
}
};
int main()
{
Solution S;
S.printNums(2);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
边栏推荐
- ROS2系列知识(3):环境配置
- Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
- GO GOPROXY proxy Settings
- 深度学习可以求解特定函数的参数么?
- [Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
- ShardingSphere之未分片表配置实战(六)
- BOM系列之history对象
- ros2知识:在单个进程中布置多个节点
- 297. 二叉树的序列化与反序列化
- Kotlin协程:协程上下文与上下文元素
猜你喜欢

深度学习可以求解特定函数的参数么?
![[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes](/img/52/88ad349eca136048acd0f328d4f33c.png)
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes

mysql索引失效的常见9种原因详解

MySQL高级-六索引优化

Meeting OA project pending meeting, all meeting functions

ShardingSphere之水平分库实战(四)

A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES

WEB Security Basics - - - Vulnerability Scanner

Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv

Preparations for web vulnerabilities
随机推荐
Adding, deleting, modifying and checking the foundation of MySQL
MySQL筑基篇之增删改查
typescript13 - type aliases
The level of ShardingSphere depots in actual combat (4)
【多线程】
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
Thesis understanding: "Designing and training of a dual CNN for image denoising"
ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)
Mysql systemized JOIN operation example analysis
SWM32 Series Tutorial 6 - Systick and PWM
【愚公系列】2022年07月 Go教学课程 013-常量、指针
Image processing tool design
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
mysql索引失效的常见9种原因详解
场景之多数据源查询及数据下载问题
网站频繁出现mysql等数据库连接失败等信息解决办法
typescript9 - common base types
xss bypass: prompt(1)
小程序-全局数据共享
Filter (Filter)