-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00012. Integer to Roman.rs
32 lines (27 loc) · 1.08 KB
/
00012. Integer to Roman.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::collections::HashMap;
impl Solution {
pub fn int_to_roman(num: i32) -> String {
// Map integers to their equivalent roman characters
let v_ints = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let v_romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
let size = v_ints.len();
let mut answer = String::new();
let mut i = 0;
let mut number = num;
let mut additions = 0;
loop {
if (i == size) {
break;
}
// If number outputs a quotient when divided by the current number, append the corresponding string of that number 'quotient' no. of times.
additions = number / v_ints[i];
if (additions != 0) {
number %= (additions * v_ints[i]);
// println!("{}", v_romans[i].repeat(additions as usize));
answer.push_str(&v_romans[i].repeat(additions as usize));
}
i += 1;
}
return answer;
}
}