+1

Leetcode Problem #2: Integer to Roman And Roman to Integer

Our problem is to convert an integer to roman, but I found another problem is to convert roman to an integer. So, I decided to create an article used to explain both those problems. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, M with details value below:

Symbol                Value
      I                    1
      V                    5
      X                    10
      L                    50
      C                    100
      D                    500
      M                    1000

For example

2 is written as II in Roman numeral
12 is written as XII = X + II
27 is written as XXVII = X + X + V + II

Roman numerals are usually written largest to smallest from left to right. . However, 4 is written as IV because I before V, we have to subtract it making 4. The same principle applies to 9, which is written by IX. There are 6 instances where subtraction is used:

IV = 4       IX = 9
XL = 40      XC = 90
CD = 400     CM = 900

So, we are going to solve each problem step by step!

Problem 1: Convert an integer to Roman

Examples:

Input: num = 3
Output: "III"
Explanation: 3 is written as III
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000 CM = 900 XC = 90 IV = 4

Now, I will use a specific number to indicate my solution step by step. Our input is 3549

  1. First, we have a loop of list value of Roman. Compare the number with each value. If the number is greater than the value, devide 3549/1000, we will have quotient is 3 and remainder is 549 --> we have MMM after this step
  2. Continue in the loop, devide 549/100 = 500 and remainder is 49 --> we have MMMD
  3. Now the number is 49, we devide 49/10 = 40 and remainder is 9 --> we have MMMDXL
  4. Last, the number is 9 -> we have MMMDXLIX

Code example (in Golang)

func ConvertIntegerToRoman(num int) string {
	result := ""
	nums := [13]int{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}
	symbols := [13]string{"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}
	for i := 12; i >= 0; i-- {
		div := num / nums[i]
		num %= nums[i]
		for j := div; j > 0; j-- {
			result += symbols[i]
		}
		if num == 0 {
			break
		}
	}
	return result
}

Problem 2: Convert the Roman to Integer

This problem is opposite to the problem 1, but we have the same idea to solve it. Because Roman numerals are usually written largest to smallest from left to right, so I will read this Roman from right to left with rule:

  • Less than the previous character -> subtract
  • Greater than the previous character -> plus

Let's check these detail steps below with example: "MCMXCIV"

Code example (In Golang)

func ConvertRomanToInteger(s string) int {
	symbols := map[string]int{
		"I": 1,
		"V": 5,
		"X": 10,
		"L": 50,
		"C": 100,
		"D": 500,
		"M": 1000,
	}

	last := 0
	res := 0

	for i := len(s) - 1; i >= 0; i-- {
		tmp := symbols[string(s[i])]
		if tmp < last {
			res -= tmp
		} else {
			res += tmp
		}

		last = tmp
	}

	return res
}

Now, we completed two problems related to Roman numerals. You can find these problem in Leetcode and try to solve them 😄.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí