12. Integer to Roman
Tags:
Medium
Skills:
Roman
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Đề bài yêu cầu chuyển integer sang số la mã
Approach
Time and space complexity
Solution
1function intToRoman(num: number): string {
2 const values: number[] = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
3 const symbols: string[] = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
4 const res: string[] = [];
5 for (let i = 0; i < values.length; i++) {
6 while (num >= values[i]) {
7 num -= values[i];
8 res.push(symbols[i])
9 }
10 }
11 return res.join('')
12};