当前位置:网站首页>剑指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
边栏推荐
- ShardingSphere read-write separation (8)
- MySQL table design for message queue to store message data
- 解决:Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- 射频器件的基本参数1
- MySQL database constraints, table design
- mysql主从复制及读写分离脚本-亲测可用
- ShardingSphere之水平分库实战(四)
- TypeScript在使用中出现的问题记录
- C language force buckles the rotating image of the 48th question.auxiliary array
- ShardingSphere's vertical sub-database sub-table actual combat (5)
猜你喜欢
typescript9 - common base types
Oracle has a weird temporary table space shortage problem
typescript12 - union types
Yolov7实战,实现网页端的实时目标检测
MySQL triggers
DOM系列之scroll系列
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
typescript11 - data types
MySQL master-slave replication and read-write separation script - pro test available
Go study notes (84) - Go project directory structure
随机推荐
MySQL database constraints, table design
权限管理怎么做的?
Why use high-defense CDN when financial, government and enterprises are attacked?
【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
金融政企被攻击为什么要用高防CDN?
ShardingSphere之垂直分库分表实战(五)
24. Please talk about the advantages and disadvantages of the singleton pattern, precautions, usage scenarios
无线模块的参数介绍和选型要点
mysql索引失效的常见9种原因详解
Oracle has a weird temporary table space shortage problem
【Multithreading】
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
typescript10-commonly used basic types
typescript17-函数可选参数
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
Niuke.com question brushing training (4)
24. 请你谈谈单例模式的优缺点,注意事项,使用场景
MySQL triggers
BOM系列之history对象