December 06, 2019
https://leetcode-cn.com/problems/integer-to-roman/
使用语言:Go
func intToRoman(num int) string {
romanMap := make(map[int]string)
romanMap[1] = "I"
romanMap[4] = "IV"
romanMap[5] = "V"
romanMap[9] = "IX"
romanMap[10] = "X"
romanMap[40] = "XL"
romanMap[50] = "L"
romanMap[90] = "XC"
romanMap[100] = "C"
romanMap[400] = "CD"
romanMap[500] = "D"
romanMap[900] = "CM"
romanMap[1000] = "M"
romans := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
var res string
for i:=0; i < len(romans); {
r := romans[i]
if num >= r {
res += romanMap[r]
num = num - r
} else {
i++
}
}
return res
}
这个题目很简单。这里我偷懒只是把所有可能的情况列出来放到一个 map 里,然后通过不断的寻找 num 的加数,来组合出罗马数字。
痕迹
没有过去,就没法认定现在的自己