- vừa được xem lúc

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

0 0 12

Người đăng: hậu nguyễn

Theo Viblo Asia

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 😄.

Bình luận

Bài viết tương tự

- vừa được xem lúc

gRPC - Nó là gì và có nên sử dụng hay không?

Nhân một ngày rảnh rỗi, mình ngồi đọc lại RPC cũng như gRPC viết lại để nhớ lâu hơn. Vấn đề là gì và tại sao cần nó .

0 0 112

- vừa được xem lúc

Embedded Template in Go

Getting Start. Part of developing a web application usually revolves around working with HTML as user interface.

0 0 40

- vừa được xem lúc

Tạo Resful API đơn giản với Echo framework và MySQL

1. Giới thiệu.

0 0 44

- vừa được xem lúc

Sử dụng goquery trong golang để crawler thông tin các website Việt Nam bị deface trên mirror-h.org

. Trong bài viết này, mình sẽ cùng mọi người khám phá một package thu thập dữ liệu có tên là goquery của golang. Mục tiêu chính của chương trình crawler này sẽ là lấy thông tin các website Việt Nam bị deface (là tấn công, phá hoại website, làm thay đổi giao diện hiển thị của một trang web, khi người

0 0 218

- vừa được xem lúc

Tạo ứng dụng craw dữ liệu bing với Golang, Mysql driver

Chào mọi người . Lâu lâu ta lại gặp nhau 1 lần, để tiếp tục series chia sẻ kiến thức về tech, hôm nay mình sẽ tìm hiểu và chia sẻ về 1 ngôn ngữ đang khá hot trong cộng đồng IT đó là Golang.

0 0 59

- vừa được xem lúc

Golang: Rest api and routing using MUX

Routing with MUX. Let's create a simple CRUD api for a blog site. # All . GET articles/ .

0 0 39