当前位置:网站首页>12. Go implementation of integer to Roman numeral and leetcode
12. Go implementation of integer to Roman numeral and leetcode
2022-07-04 00:27:00 【Li Jue】
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 :
I Can be placed in V (5) and X (10) Left side , To express 4 and 9.
X Can be placed in L (50) and C (100) Left side , To express 40 and 90.
C Can be placed in D (500) and M (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"
Example 3:
Input : num = 9
Output : "IX"
Example 4:
Input : num = 58
Output : "LVIII"
explain : L = 50, V = 5, III = 3.
Example 5:
Input : num = 1994
Output : "MCMXCIV"
explain : M = 1000, CM = 900, XC = 90, IV = 4.
Tips :
1 <= num <= 3999
Traverse :
func intToRoman(num int) string {
m := map[int]string{
1: "I",
5: "V",
10: "X",
50: "L",
100: "C",
500: "D",
1000: "M",
}
m[4]="IV"
m[9]="IX"
m[40]="XL"
m[90]="XC"
m[400]="CD"
m[900]="CM"
arr := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
res:= ""
for i:=0;i<len(arr);i++{
if num==0 {
break
}
val:=num/arr[i]
for j:=0;j<val;j++{
res = res + m[arr[i]]
}
num=num-val*arr[i]
}
return res
}You can get from the prompt 1 <= num <= 3999, You can use enumeration , take num Enumerate the ten thousand bits of
func intToRoman(num int) string {
res:=""
ge := []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} //0-9
shi := []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"} //0-9
bai := []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} //0-9
qian := []string{"", "M", "MM", "MMM"} //0-3
res = qian[num/1000]+bai[num%1000/100]+shi[num%100/10]+ge[num%10]
return res
}边栏推荐
- Selenium library 4.5.0 keyword explanation (II)
- MySQL is installed as a Windows Service
- Eight year test old bird, some suggestions for 1-3 year programmers
- Development and application of fcitx functional plug-ins
- Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
- SQL data update
- Detailed explanation of the relationship between Zhongtai, wechat and DDD
- Speed up the energy Internet of things. What can low-power Internet of things technology represented by Zeta do?
- [leetcode] interview question 17.08 Circus tower
- Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
猜你喜欢

Pytorch learning notes 5: model creation

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders

Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
![[source code] VB6 chat robot](/img/89/46b67f627c8257eaddc70a247c9ba5.jpg)
[source code] VB6 chat robot

MySQL is installed as a Windows Service

NLP Chinese corpus project: large scale Chinese natural language processing corpus

Tencent interview: can you find the number of 1 in binary?

Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at

On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
随机推荐
P1339 [USACO09OCT]Heat Wave G
system. Exit (0) and system exit(1)
It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
NLP Chinese corpus project: large scale Chinese natural language processing corpus
How will the complete NFT platform work in 2022? How about its core functions and online time?
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
BBS forum recommendation
Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
It is forbidden to splice SQL in code
Gossip about redis source code 81
2022 Software Test Engineer skill list, please check
Solution to the impact of Remote Code Execution Vulnerability of log4j2 component on December 9, 2021
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
The difference between objects and objects
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Gossip about redis source code 82
Gossip about redis source code 74
Introducing Software Testing
Is the securities account opened by Caicai for individuals safe? Is there a routine