当前位置:网站首页>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
}
边栏推荐
- It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
- Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
- China standard gas market prospect investment and development feasibility study report 2022-2028
- Why use get/set instead of exposing properties
- Interview script of Software Test Engineer
- [leetcode] interview question 17.08 Circus tower
- Generic
- Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
- Gossip about redis source code 80
- Is the account opening of Guoyuan securities really safe and reliable
猜你喜欢
Qtcharts notes (V) scatter diagram qscatterseries
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
[Mongodb] 2. Use mongodb --------- use compass
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Entropy and full connection layer
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
SPI based on firmware library
Att & CK actual combat series - red team actual combat - V
Is user authentication really simple
Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
随机推荐
Subgraph isomorphism -subgraph isomorphism
Detailed explanation of the relationship between Zhongtai, wechat and DDD
1214 print diamond
Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
Ramble 72 of redis source code
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
Gossip about redis source code 75
Introducing Software Testing
Development and application of fcitx functional plug-ins
[GNN] hard core! This paper combs the classical graph network model
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
【leetcode】374. Guess the size of the number
2022 system integration project management engineer examination knowledge points: software development model
Analysis: misunderstanding of choosing WMS warehouse management system
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
Data storage - interview questions
Docking Alipay process [pay in person, QR code Payment]
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
MySQL winter vacation self-study 2022 12 (1)