当前位置:网站首页>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
}
边栏推荐
- Several ways to set up a blog locally [attach relevant software download links]
- Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
- Idea integrates Microsoft TFs plug-in
- [PHP basics] cookie basics, application case code and attack and defense
- Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
- 2020.2.14
- Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
- How to trade spot gold safely?
- ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
- 1214 print diamond
猜你喜欢
STM32 GPIO CSDN creative punch in
[Mongodb] 2. Use mongodb --------- use compass
What is the future of software testing industry? Listen to the test veterans' answers
Analysis: misunderstanding of choosing WMS warehouse management system
Entropy and full connection layer
Double efficiency. Six easy-to-use pychar plug-ins are recommended
Correlation analysis summary
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
What is the potential of pocket network, which is favored by well-known investors?
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
随机推荐
Generic
[source code] VB6 chat robot
Alibaba cloud container service differentiation SLO hybrid technology practice
Version rollback revert don't reset better reset must be forced
Idea set class header comments
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
Gossip about redis source code 82
STM32 GPIO CSDN creative punch in
[CSDN Q & A] experience and suggestions
Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
HR disgusted interview behavior
Regular expressions and text processors for shell programming
国元证券开户是真的安全可靠吗
Struct in linked list
How will the complete NFT platform work in 2022? How about its core functions and online time?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
SPI based on firmware library
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
Analysis: misunderstanding of choosing WMS warehouse management system