Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5593346

Browse files
solve: 12. Integer to Roman in rust & golang
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent b5c2893 commit 5593346

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
func intToRoman(num int) string {
4+
ones := []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
5+
tens := []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
6+
hundreds := []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
7+
thousands := []string{"", "M", "MM", "MMM"}
8+
9+
thousand := num / 1000
10+
hundred := (num / 100) % 10
11+
ten := (num / 10) % 10
12+
one := num % 10
13+
14+
return thousands[thousand] + hundreds[hundred] + tens[ten] + ones[one]
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const ONES: [&str; 10] = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
2+
const TENS: [&str; 10] = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
3+
const HUNDS: [&str; 10] = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
4+
const THOUSANDS: [&str; 4] = ["", "M", "MM", "MMM"];
5+
6+
impl Solution {
7+
pub fn int_to_roman(num: i32) -> String {
8+
let num = num as usize;
9+
10+
format!(
11+
"{}{}{}{}",
12+
THOUSANDS[(num / 1000)],
13+
HUNDS[(num / 100 % 10)],
14+
TENS[(num / 10 % 10)],
15+
ONES[(num % 10)]
16+
)
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /