当前位置:网站首页>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)
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
猜你喜欢

Turn: Management is the love of possibility, and managers must have the courage to break into the unknown

看DevExpress丰富图表样式,如何为基金公司业务创新赋能

小程序 + 电商,玩转新零售

7-1 LVS+NAT load balancing cluster, NAT mode deployment

42. 接雨水

There is an 8 hour difference between the docker installation of mysql and the host.

转:管理是对可能性的热爱,管理者要有闯进未知的勇气

SVM介绍以及实战

系统设计.如何设计一个秒杀系统(完整版 转)

docker安装mysql与宿主机相差8小时的问题。
随机推荐
How class only static allocation and dynamic allocation
【21天学习挑战赛】图像的旋转问题(二维数组)
深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
深度学习21天——准备(环境配置)
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
深度学习之 10 卷积神经网络3
PL/SQL Some Advanced Fundamental
2022年PMP考试延迟了,该喜该忧?
Large chain best freight d audit with what software?What are the functions?
Shocked, 99.9% of the students didn't really understand the immutability of strings
应届生软件测试薪资大概多少?
C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
Introduction to mq application scenarios
7-2 LVS+DR Overview and Deployment
Cache pool of unity framework
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
大型连锁百货运维审计用什么软件好?有哪些功能?
System design. Seckill system