当前位置:网站首页>leetcode 12. Integer to Roman numeral
leetcode 12. Integer to Roman numeral
2022-08-04 04:47:00 【_Liu Xiaoyu】
作者简介:C/C++ 、Golang 领域耕耘者,创作者
个人主页:作者主页
活动地址:CSDN21天学习挑战赛
题目来源: leetcode官网
如果感觉博主的文章还不错的话,还请关注 、点赞 、收藏🧡三连支持一下博主哦~~~
题目描述
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II .
通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 .同样地,数字 9 表示为 IX.这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9.
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90.
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900.
给你一个整数,将其转为罗马数字.
示例1:
输入: num = 3
输出: “III”
示例2:
输入: num = 4
输出: “IV”
示例3:
输入: num = 9
输出: “IX”
示例4:
输入: num = 58
输出: “LVIII”
解释: L = 50, V = 5, III = 3.
示例5:
输入: num = 1994
输出: “MCMXCIV”
解释: M = 1000, CM = 900, XC = 90, IV = 4.
🧡 算法分析
This problem is to simulate the topic
The first is the basic Roman Numbers(The above table in below)
We can get some laws,
- 相同的数字连写,所表示的数等于这些数字相加得到的数,如:III=3;
- 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如:VIII=8, XII=12;
- 小的数字在大的数字的左边(限于 IV、IX、XL、XC、CD和CM),所表示的数等于大数减小数得到的数,如:IV=4, IX=9;
- 正常使用时,连写的数字重复不得超过三次;
So we can use a new composite characters on behalf of the new unit, In the above table below
模拟实现步骤:
- To organize new units and numerical array
- Directly on the need to convert Numbers to iterate over the(Began to give preference to the larger units)
代码实现
class Solution {
public:
string intToRoman(int num) {
// 属于模拟题
int vals[] = {
1000,
900,500,400,100,
90,50,40,10,
9,5,4,1
};
string reps[] = {
"M",
"CM", "D", "CD", "C",
"XC", "L", "XL", "X",
"IX", "V", "IV", "I"
};
string re;
// To choose the big unit
for(int i = 0; i < 13; i++) // Because the above array there is13个数
{
while(num >= vals[i])
{
num -= vals[i];
re += reps[i];
}
}
return re;
}
};
执行结果:
时间复杂度分析
计算量与最终罗马数字的长度成正比,对于每一位阿拉伯数字,罗马数字最多用4个字母表示(比如VIII=8),So the length of the Roman numerals and the length of the Arabic numerals are an order of magnitude,And the length of the Arabic numerals is proportional to the number,是 O(logn)
,因此时间复杂度是 O(logn)
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- 7.LVS负载均衡群集之原理叙述
- 8.Haproxy 搭建Web集群
- [Skill] Using Sentinel to achieve priority processing of requests
- C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
- Tensors - Application Cases
- Large chain best freight d audit with what software?What are the functions?
- 使用serve搭建本地服务器
- 深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
- [C language advanced] program environment and preprocessing
- How to simplify the automation of modern e-procurement?
猜你喜欢
附加:对于“与数据表对应的实体类“,【面对MongoDB时,使用的@Id等注解】和【以前面对MySQL时,使用的@Id等注解】,是不同的;
文件系统的简单操作
Introduction to mq application scenarios
解决错误:npm WARN config global `--global`, `--local` are deprecated
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
For Qixi Festival, I made a confession envelope with code
drools from download to postman request success
7-2 LVS+DR Overview and Deployment
7-1 LVS+NAT 负载均衡群集,NAT模式部署
使用Loadrunner进行性能测试
随机推荐
Mobile payment online and offline payment scenarios
Eight guiding principles to help businesses achieve digital transformation success
7.LVS负载均衡群集之原理叙述
42. 接雨水
2023年PMP考试会用新版教材吗?回复来了!
小程序 + 电商,玩转新零售
数据治理平台项目总结和分析
8. Haproxy builds a web cluster
【一步到位】Jenkins的安装、部署、启动(完整教程)
深度学习之 10 卷积神经网络3
深度学习环境配置
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
TL431的基本特性以及振荡电路
XSS related knowledge points
使用serve搭建本地服务器
Cache pool of unity framework
Mini program + e-commerce, fun new retail
mysql索引笔记