LeetCode: Integer To Roman Solution
1/**2 * @param {number} num3 * @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 }2122 let res = ""23 for (const [key, value] of Object.entries(dict).reverse()) {24 res += key.repeat(Math.floor(num / value))25 num %= value26 }2728 return res29}
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
Apr 3, 2021
Previous Post
LeetCode: Swapping Nodes In A Linked List
Mar 31, 2021