当前位置:网站首页>Leetcode-12- integer to Roman numeral (medium)
Leetcode-12- integer to Roman numeral (medium)
2022-06-13 00:59:00 【Didi dada】
12. Integer to Roman number ( secondary )
Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
for example , Rome digital 2 Write to do II , Two parallel 1.12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
ICan be placed inV(5) andX(10) Left side , To express 4 and 9.XCan be placed inL(50) andC(100) Left side , To express 40 and 90.CCan be placed inD(500) andM(1000) Left side , To express 400 and 900.
Give you an integer , Turn it into Roman numerals .
Example 1:
Input : num = 3
Output : "III"
Example 2:
Input : num = 4
Output : "IV"
Ideas :
According to the representation of Roman numerals , Until the Roman characters have ”M”, “CM”, “D”, “CD”, “C”, “XC”, “L”, “XL”, “X”,“IX”, “V”, “IV”, “I” These kinds of , They correspond to each other 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1. If between these numbers , such as 3, Then repeat 3 Write again 1: III. So we just need to follow the Roman numeral from big to small , When num Than a[i] When I was big , cnt = num / a[i], will b[i] Connected to the s character string 串 Back cnt Time . Each cycle will num Remainder a[i], until num<=0 until ~
(1)C++
class Solution {
public:
string intToRoman(int num) {
int a[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4,1};
string b[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X","IX", "V", "IV", "I"};
string s = "";
int index = 0, cnt;
while(num > 0) {
cnt = num / a[index];
while(cnt--)
s += b[index];
num = num % a[index];
index++;
}
return s;
}
};
(2)python
class Solution:
def intToRoman(self, num: int) -> str:
a = [(1000,"M"), (900,"CM"), (500,"D"), (400,"CD"), (100,"C"), (90,"XC"), (50,"L"), (40,"XL"), (10,"X"), (9,"IX"), (5,"V"), (4,"IV"), (1,"I")]
s = ''
while num:
for each, value in a:
cnt = num//each
while(cnt):
s+=value
cnt-=1
num = num%each
return s
边栏推荐
- Opencv face recognition of ros2 foxy~galactic~humble
- 深度学习训练多少轮?迭代多少次?
- 深度学习模型剪枝
- Antdpro - protable realizes the linkage effect of two selection boxes
- pytorch是什么?解释pytorch的基本概念
- [JS component] customize the right-click menu
- ROS从入门到精通(零) 教程导读
- Five classic articles worth reading (2)
- Higherhrnet pre training model -- download from network disk
- [Latex] 插入圖片
猜你喜欢

什么是 Meebits?一个简短的解释

With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?

Et5.0 value type generation

什么是 dummy change?

Introduction to ROS from introduction to mastery (zero) tutorial

Win10 home vs pro vs enterprise vs enterprise LTSC

How many rounds of deep learning training? How many iterations?
![[Latex] 插入图片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入图片

Pipeline流水线项目构建

Kotlin 协程的作用域构建器 coroutineScope与runBlocking 与supervisorScope,协程同步运行,协程挂掉的时候其他协程如何不被挂掉。
随机推荐
[JS component] calendar
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
Illustrator tutorial, how to add dashes and arrows in illustrator?
Unity extension
The grass is bearing seeds
About_ int128
单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
sort
Binary tree - right view
gpu加速pytorch能用吗?
软件测试的几种分类,一看就明了
Canvas game 2048 free map size
Oceanbase is the leader in the magic quadrant of China's database in 2021
Et5.0 value type generation
304. Merge two ordered arrays
Androi天气
Four startup modes of kotlin collaboration
Pipeline流水线项目构建
[backtrader source code analysis 7] analysis of the functions for calculating mean value, variance and standard deviation in mathsupport in backtrader (with low gold content)
Five classic articles worth reading