Skip to content

Commit e7de1ab

Browse files
committed
12. Integer To Roman
```Solution.c char* intToRoman(int num) { static char res[20]; int val[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; char* sy[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int i = 0, p = 0; while (num) { while (num >= val[i]) { for (char* s = sy[i]; *s; s++) res[p++] = *s; num -= val[i]; } i++; } res[p] = 0; return res; } ``` ```Solution.cpp class Solution { public: string intToRoman(int num) { vector<pair<int, string>> val = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},{100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},{10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}}; string res = ""; for (auto &[v, s] : val) { while (num >= v) { res += s; num -= v; } } return res; } }; ``` ```Solution.cs public class Solution { public string IntToRoman(int num) { int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; string[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL","X", "IX", "V", "IV", "I"}; var sb = new StringBuilder(); for (int i = 0; i < values.Length; i++) { while (num >= values[i]) { sb.Append(symbols[i]); num -= values[i]; } } return sb.ToString(); } } ``` ```Solution.dart class Solution { String intToRoman(int num) { List<int> values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; List<String> symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; StringBuffer res = StringBuffer(); for (int i = 0; i < values.length; i++) { while (num >= values[i]) { res.write(symbols[i]); num -= values[i]; } } return res.toString(); } } ``` ```Solution.erl -spec int_to_roman(Num :: integer()) -> unicode:unicode_binary(). int_to_roman(Num) -> int_to_roman(Num, [{1000, <<"M">>}, {900, <<"CM">>}, {500, <<"D">>}, {400, <<"CD">>}, {100, <<"C">>}, {90, <<"XC">>}, {50, <<"L">>}, {40, <<"XL">>}, {10, <<"X">>}, {9, <<"IX">>}, {5, <<"V">>}, {4, <<"IV">>}, {1, <<"I">>}] ). int_to_roman(0, _) -> <<>>; int_to_roman(Num, [{Value, Roman} | Rest]) when Num >= Value -> <<Roman/binary, (int_to_roman(Num - Value, [{Value, Roman} | Rest]))/binary>>; int_to_roman(Num, [_ | Rest]) -> int_to_roman(Num, Rest). ``` ```Solution.ex defmodule Solution do def int_to_roman(num) do values = [{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}] Enum.reduce(values, {num, ""}, fn {val, sym}, {n, acc} -> count = div(n, val) {rem(n, val), acc <> String.duplicate(sym, count)} end) |> elem(1) end end ``` ```Solution.go func intToRoman(num int) string { values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1} symbols := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"} res := "" for i := 0; i < len(values); i++ { for num >= values[i] { res += symbols[i] num -= values[i] } } return res } ``` ```Solution.java class Solution { public String intToRoman(int num) { int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuilder sb = new StringBuilder(); for (int i = 0; i < values.length; i++) { while (num >= values[i]) { sb.append(symbols[i]); num -= values[i]; } } return sb.toString(); } } ``` ```Solution.js /** * @param {number} num * @return {string} */ var intToRoman = function(num) { const val = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']]; let res = ''; for (const [v, s] of val) { while (num >= v) { res += s; num -= v; } } return res; }; ``` ```Solution.kt class Solution { fun intToRoman(num: Int): String { val values = arrayOf(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) val symbols = arrayOf("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I") var n = num val sb = StringBuilder() for (i in values.indices) { while (n >= values[i]) { sb.append(symbols[i]) n -= values[i] } } return sb.toString() } } ``` ```Solution.php class Solution { /** * @param Integer $num * @return String */ function intToRoman($num) { $values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; $symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; $res = ""; for ($i = 0; $i < count($values); $i++) { while ($num >= $values[$i]) { $res .= $symbols[$i]; $num -= $values[$i]; } } return $res; } } ``` ```Solution.py class Solution(object): def intToRoman(self, num): val = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')] res = '' for v, s in val: while num >= v: res += s num -= v return res ``` ```Solution.rb # @param {Integer} num # @return {String} def int_to_roman(num) val = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']] res = '' val.each do |v, s| while num >= v res += s num -= v end end res end ``` ```Solution.rkt (define (int-to-roman num) (define pairs '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD") (100 . "C") (90 . "XC") (50 . "L") (40 . "XL") (10 . "X") (9 . "IX") (5 . "V") (4 . "IV") (1 . "I"))) (define (helper n lst acc) (if (or (zero? n) (null? lst)) acc (let* ((pair (car lst)) (val (car pair)) (sym (cdr pair))) (if (>= n val) (helper (- n val) lst (string-append acc sym)) (helper n (cdr lst) acc))))) (helper num pairs "")) ``` ```Solution.rs impl Solution { pub fn int_to_roman(mut num: i32) -> String { let values = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")]; let mut result = String::new(); for &(val, sym) in values.iter() { while num >= val { result.push_str(sym); num -= val; } } result } } ``` ```Solution.scala object Solution { def intToRoman(num: Int): String = { val values = List(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) val symbols = List("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I") var n = num val sb = new StringBuilder for (i <- values.indices) { while (n >= values(i)) { sb.append(symbols(i)) n -= values(i) } } sb.toString } } ``` ```Solution.swift class Solution { func intToRoman(_ num: Int) -> String { let values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] let symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] var num = num var result = "" for (i, value) in values.enumerated() { while num >= value { result += symbols[i] num -= value } } return result } } ``` ```Solution.ts function intToRoman(num: number): string { const val: [number, string][] = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']]; let res = ''; for (const [v, s] of val) { while (num >= v) { res += s; num -= v; } } return res; } ```
1 parent 5871d7e commit e7de1ab

19 files changed

+300
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [**12. Integer To Roman**](https://leetcode.com/problems/integer-to-roman/description/)
2+
3+
Seven different symbols represent Roman numerals with the following values:
4+
5+
| Symbol | Value |
6+
|--------|-------|
7+
| I | 1 |
8+
| V | 5 |
9+
| X | 10 |
10+
| L | 50 |
11+
| C | 100 |
12+
| D | 500 |
13+
| M | 1000 |
14+
15+
Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
16+
- If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
17+
- If the value starts with 4 or 9 use the **subtractive form** representing one symbol subtracted from the following symbol, for example, 4 is 1 (`I`) less than 5 (`V`): `IV` and 9 is 1 (`I`) less than 10 (`X`): `IX`. Only the following subtractive forms are used: 4 (`IV`), 9 (`IX`), 40 (`XL`), 90 (`XC`), 400 (`CD`) and 900 (`CM`).
18+
- Only powers of 10 (`I`, `X`, `C`, `M`) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (`V`), 50 (`L`), or 500 (`D`) multiple times. If you need to append a symbol 4 times use the **subtractive form**.
19+
20+
Given an integer, convert it to a Roman numeral.
21+
22+
#### **Example 1:**
23+
```md
24+
Input: num = 3749
25+
Output: "MMMDCCXLIX"
26+
Explanation:
27+
3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
28+
700 = DCC as 500 (D) + 100 (C) + 100 (C)
29+
40 = XL as 10 (X) less of 50 (L)
30+
9 = IX as 1 (I) less of 10 (X)
31+
Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
32+
```
33+
34+
#### **Example 2:**
35+
```md
36+
Input: num = 58
37+
Output: "LVIII"
38+
Explanation:
39+
50 = L
40+
8 = VIII
41+
```
42+
43+
#### **Example 3:**
44+
```md
45+
Input: num = 1994
46+
Output: "MCMXCIV"
47+
Explanation:
48+
1000 = M
49+
900 = CM
50+
90 = XC
51+
4 = IV
52+
```
53+
54+
#### **Constraints:**
55+
> - `1 <= num <= 3999`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
char* intToRoman(int num) {
2+
static char res[20];
3+
int val[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
4+
char* sy[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
5+
int i = 0, p = 0;
6+
while (num) {
7+
while (num >= val[i]) {
8+
for (char* s = sy[i]; *s; s++) res[p++] = *s;
9+
num -= val[i];
10+
}
11+
i++;
12+
}
13+
res[p] = 0;
14+
return res;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string intToRoman(int num) {
4+
vector<pair<int, string>> val = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},{100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},{10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}};
5+
string res = "";
6+
for (auto &[v, s] : val) {
7+
while (num >= v) {
8+
res += s;
9+
num -= v;
10+
}
11+
}
12+
return res;
13+
}
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public string IntToRoman(int num) {
3+
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
4+
string[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL","X", "IX", "V", "IV", "I"};
5+
var sb = new StringBuilder();
6+
for (int i = 0; i < values.Length; i++) {
7+
while (num >= values[i]) {
8+
sb.Append(symbols[i]);
9+
num -= values[i];
10+
}
11+
}
12+
return sb.ToString();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
String intToRoman(int num) {
3+
List<int> values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
4+
List<String> symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
5+
StringBuffer res = StringBuffer();
6+
for (int i = 0; i < values.length; i++) {
7+
while (num >= values[i]) {
8+
res.write(symbols[i]);
9+
num -= values[i];
10+
}
11+
}
12+
return res.toString();
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-spec int_to_roman(Num :: integer()) -> unicode:unicode_binary().
2+
int_to_roman(Num) ->
3+
int_to_roman(Num,
4+
[{1000, <<"M">>}, {900, <<"CM">>}, {500, <<"D">>}, {400, <<"CD">>},
5+
{100, <<"C">>}, {90, <<"XC">>}, {50, <<"L">>}, {40, <<"XL">>},
6+
{10, <<"X">>}, {9, <<"IX">>}, {5, <<"V">>}, {4, <<"IV">>}, {1, <<"I">>}]
7+
).
8+
int_to_roman(0, _) ->
9+
<<>>;
10+
int_to_roman(Num, [{Value, Roman} | Rest]) when Num >= Value ->
11+
<<Roman/binary, (int_to_roman(Num - Value, [{Value, Roman} | Rest]))/binary>>;
12+
int_to_roman(Num, [_ | Rest]) ->
13+
int_to_roman(Num, Rest).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule Solution do
2+
def int_to_roman(num) do
3+
values = [{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}]
4+
Enum.reduce(values, {num, ""}, fn {val, sym}, {n, acc} ->
5+
count = div(n, val)
6+
{rem(n, val), acc <> String.duplicate(sym, count)}
7+
end)
8+
|> elem(1)
9+
end
10+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func intToRoman(num int) string {
2+
values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
3+
symbols := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
4+
res := ""
5+
for i := 0; i < len(values); i++ {
6+
for num >= values[i] {
7+
res += symbols[i]
8+
num -= values[i]
9+
}
10+
}
11+
return res
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public String intToRoman(int num) {
3+
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
4+
String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
5+
StringBuilder sb = new StringBuilder();
6+
for (int i = 0; i < values.length; i++) {
7+
while (num >= values[i]) {
8+
sb.append(symbols[i]);
9+
num -= values[i];
10+
}
11+
}
12+
return sb.toString();
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} num
3+
* @return {string}
4+
*/
5+
var intToRoman = function(num) {
6+
const val = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']];
7+
let res = '';
8+
for (const [v, s] of val) {
9+
while (num >= v) {
10+
res += s;
11+
num -= v;
12+
}
13+
}
14+
return res;
15+
};

0 commit comments

Comments
 (0)