当前位置:网站首页>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
}边栏推荐
- JDBC Technology
- 【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
- Pytorch learning notes 5: model creation
- Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
- Alibaba cloud container service differentiation SLO hybrid technology practice
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
- Several ways to set up a blog locally [attach relevant software download links]
- ITK learning notes (VII) the position of ITK rotation direction remains unchanged
- Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

2022 system integration project management engineer examination knowledge points: software development model

Detailed explanation of the relationship between Zhongtai, wechat and DDD

Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools

JDBC Technology
![[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections](/img/c6/3dc7d01600f6713afdbb4cf3df5238.jpg)
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections

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

(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes

Smart fan system based on stm32f407

Entropy and full connection layer

Generic
随机推荐
AI Challenger 2018 text mining competition related solutions and code summary
[cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept
Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
What are the application fields of digital twins in industry?
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
Alibaba cloud container service differentiation SLO hybrid technology practice
Introducing Software Testing
Iclr2022: how does AI recognize "things I haven't seen"?
China standard gas market prospect investment and development feasibility study report 2022-2028
URL (data:image/png; Base64, ivborw0k... Use case
Detailed explanation of the relationship between Zhongtai, wechat and DDD
Gossip about redis source code 79
1214 print diamond
Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
Att & CK actual combat series - red team actual combat - V
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
Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
[CSDN Q & A] experience and suggestions
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
NLP pre training technology development