当前位置:网站首页>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
}边栏推荐
- Briefly understand the operation mode of developing NFT platform
- Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
- Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
- The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
- Reading notes on how programs run
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
- Qtcharts notes (V) scatter diagram qscatterseries
- What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
- STM32 key light
- system. Exit (0) and system exit(1)
猜你喜欢

Struct in linked list

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

Shell script three swordsman sed

On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
![Several ways to set up a blog locally [attach relevant software download links]](/img/2f/51a09d9ef71065319ed90306517854.jpg)
Several ways to set up a blog locally [attach relevant software download links]

Is user authentication really simple

Sorry, Tencent I also refused

Test the influence of influent swacth on the electromagnetic coil of quartz meter

China standard gas market prospect investment and development feasibility study report 2022-2028

What is the future of software testing industry? Listen to the test veterans' answers
随机推荐
For loop
Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
Selenium library 4.5.0 keyword explanation (II)
Gossip about redis source code 74
1214 print diamond
Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
How to make recv have a little temper?
Similarities and differences of text similarity between Jaccard and cosine
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
Makefile judge custom variables
Why use get/set instead of exposing properties
Double efficiency. Six easy-to-use pychar plug-ins are recommended
[source code] VB6 chat robot
What is the potential of pocket network, which is favored by well-known investors?
What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
Interview script of Software Test Engineer
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
Subgraph isomorphism -subgraph isomorphism