https://leetcode.cn/problems/roman-to-integer/description/?envType=study-plan-v2&envId=top-interview-150
GO
package leetcode150 import "testing" /* romanMap := map[string]int{ "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, } */ func TestRomanToInt(t *testing.T) { s := "MCMXCIV" res := romanToInt(s) println(res) } func romanToInt(s string) int { total := 0 for i := 0; i < len(s); i++ { switch s[i] { case 'I': if i+1 < len(s) && s[i+1] == 'V' { total += 4 i++ } else if i+1 < len(s) && s[i+1] == 'X' { total += 9 i++ } else { total += 1 } case 'V': total += 5 case 'X': if i+1 < len(s) && s[i+1] == 'L' { total += 40 i++ } else if i+1 < len(s) && s[i+1] == 'C' { total += 90 i++ } else { total += 10 } case 'L': total += 50 case 'C': if i+1 < len(s) && s[i+1] == 'D' { total += 400 i++ } else if i+1 < len(s) && s[i+1] == 'M' { total += 900 i++ } else { total += 100 } case 'D': total += 500 case 'M': total += 1000 } } return total }
标签:case,150,13,++,len,else,&&,total,leetcode From: https://www.cnblogs.com/MoonBeautiful/p/18363772