当前位置:网站首页>剑指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
边栏推荐
- 分布式.分布式锁
- 论文理解:“Designing and training of a dual CNN for image denoising“
- 深度学习可以求解特定函数的参数么?
- In Google Cloud API gateway APISIX T2A and T2D performance test
- Filter (Filter)
- Image processing tool design
- Problem record in the use of TypeScript
- WMware Tools安装失败segmentation fault解决方法
- 35. 反转链表
- 查看zabbix-release-5.0-1.el8.noarch.rpm包内容
猜你喜欢
Mysql systemized JOIN operation example analysis
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
Detailed explanation of 9 common reasons for MySQL index failure
不用Swagger,那我用啥?
Rocky/GNU之Zabbix部署(2)
xss bypass: prompt(1)
typescript9-常用基础类型
MySQL master-slave replication and read-write separation script - pro test available
MySQL高级-六索引优化
一万了解 Gateway 知识点
随机推荐
这个项目太有极客范儿了
Meeting OA project pending meeting, all meeting functions
什么是Promise?Promise的原理是什么?Promise怎么用?
一万了解 Gateway 知识点
MySQL筑基篇之增删改查
What is Promise?What is the principle of Promise?How to use Promises?
Rocky/GNU之Zabbix部署(3)
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
查看zabbix-release-5.0-1.el8.noarch.rpm包内容
GO GOPROXY代理设置
Typescript18 - object type
Adding, deleting, modifying and checking the foundation of MySQL
typescript15-(同时指定参数和返回值类型)
射频器件的基本参数1
论文理解:“Designing and training of a dual CNN for image denoising“
分布式系统的一致性与共识(1)-综述
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
网站频繁出现mysql等数据库连接失败等信息解决办法
Rocky/GNU之Zabbix部署(2)
C language force buckles the rotating image of the 48th question.auxiliary array