LeetCode: Integer To Roman Solution

1/**
2 * @param {number} num
3 * @return {string}
4 */
5var intToRoman = function (num) {
6 const dict = {
7 I: 1,
8 IV: 4,
9 V: 5,
10 IX: 9,
11 X: 10,
12 XL: 40,
13 L: 50,
14 XC: 90,
15 C: 100,
16 CD: 400,
17 D: 500,
18 CM: 900,
19 M: 1000,
20 }
21
22 let res = ""
23 for (const [key, value] of Object.entries(dict).reverse()) {
24 res += key.repeat(Math.floor(num / value))
25 num %= value
26 }
27
28 return res
29}

Comments

Loading comments...

Tags

leetcode

string

math

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Longest Valid Parentheses

HoningJS

Search Posts