当前位置:网站首页>The sword refers to offer17---print the n digits from 1 to the largest
The sword refers to offer17---print the n digits from 1 to the largest
2022-07-31 00:58:00 【Starlight Technician】
打印从1到最大的n位数

题目考点:The original title set the array againINT32的范围内,所以可以直接使用for循环依次求出;But if the value exceeds INT32,就需要使用long型;如果数字比longwhat about bigger;无论是 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
边栏推荐
- Responsive layout vs px/em/rem
- Jmeter参数传递方式(token传递,接口关联等)
- typescript12-联合类型
- Detailed explanation of 9 common reasons for MySQL index failure
- The level of ShardingSphere depots in actual combat (4)
- ShardingSphere之水平分库实战(四)
- 【愚公系列】2022年07月 Go教学课程 019-循环结构之for
- Rocky/GNU之Zabbix部署(3)
- What is Promise?What is the principle of Promise?How to use Promises?
- typescript13-类型别名
猜你喜欢

The level of ShardingSphere depots in actual combat (4)

Mysql:Invalid default value for TIMESTAMP

射频器件的基本参数1

VS warning LNK4099:未找到 PDB 的解决方案

这个项目太有极客范儿了

无线模块的参数介绍和选型要点

MySQL高级-六索引优化

Jmeter parameter transfer method (token transfer, interface association, etc.)

Shell programming of conditional statements

解析云原生消息流系统 Apache Pulsar 能力及场景
随机推荐
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
The client series of the DOM series
Shell编程之条件语句
ABC 261 F - Sorting Color Balls (reverse pair)
Mysql:Invalid default value for TIMESTAMP
background has no effect on child elements of float
ELK deployment script---pro test available
DOM系列之scroll系列
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
【c语言课程设计】C语言校园卡管理系统
Niuke.com question brushing training (4)
分布式.分布式锁
ShardingSphere之公共表实战(七)
MySQL master-slave replication and read-write separation script - pro test available
87. 把字符串转换成整数
MySQL database constraints, table design
redis学习
What is Promise?What is the principle of Promise?How to use Promises?
Shell programming of conditional statements
不用Swagger,那我用啥?