当前位置:网站首页>剑指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
边栏推荐
- Basic usage of async functions and await expressions in ES6
- ShardingSphere之垂直分库分表实战(五)
- 场景之多数据源查询及数据下载问题
- [In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
- typescript10-常用基础类型
- WEB Security Basics - - - Vulnerability Scanner
- What is Promise?What is the principle of Promise?How to use Promises?
- Rocky/GNU之Zabbix部署(1)
- How to Add a Navigation Menu on Your WordPress Site
- Shell programming of conditional statements
猜你喜欢
go mode tidy出现报错go warning “all“ matched no packages
ShardingSphere read-write separation (8)
C language force buckles the rotating image of the 48th question.auxiliary array
typescript17 - function optional parameters
ShardingSphere之公共表实战(七)
Sping.事务的传播特性
typescript9-常用基础类型
【ABAP】MFBF过账到质量检验库存类型Demo
ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)
In Google Cloud API gateway APISIX T2A and T2D performance test
随机推荐
This project is so geeky
(5) fastai application
typescript9 - common base types
Common network status codes
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
这个项目太有极客范儿了
ShardingSphere之未分片表配置实战(六)
Typescript14 - (type) of the specified parameters and return values alone
论文理解:“Designing and training of a dual CNN for image denoising“
ELK部署脚本---亲测可用
系统设计.短链系统设计
图像处理工具设计
MySQL database constraints, table design
【愚公系列】2022年07月 Go教学课程 013-常量、指针
WMware Tools安装失败segmentation fault解决方法
响应式布局与px/em/rem的比对
射频器件的基本参数1
BOM系列之history对象
typescript18-对象类型
TypeScript在使用中出现的问题记录