leetcode_rust/problems/p000_0xx/
p000_012.rs

1//! # Description
2//!
3//! Roman numerals are represented by seven different symbols: `I`, `V`, `X`, 
4//! `L`, `C`, `D` and `M`.
5//! 
6//! ```plain
7//! Symbol       Value
8//! I            1
9//! V            5
10//! X            10
11//! L            50
12//! C            100
13//! D            500
14//! M            1000
15//! ```
16//! For example, `2` is written as `II` in Roman numeral, just two one's added 
17//! together. `12` is written as `XII`, which is simply `X` + `II`. The number 
18//! `27` is written as `XXVII`, which is `XX` + `V` + `II`.
19//! 
20//! Roman numerals are usually written largest to smallest from left to right. 
21//! However, the numeral for four is not `IIII`. Instead, the number four is 
22//! written as `IV`. Because the one is before the five we subtract it making 
23//! four. The same principle applies to the number nine, which is written as 
24//! `IX`. There are six instances where subtraction is used:
25//! 
26//! `I` can be placed before `V` (`5`) and `X` (`10`) to make `4` and `9`. 
27//! `X` can be placed before `L` (`50`) and `C` (`100`) to make `40` and `90`. 
28//! `C` can be placed before `D` (`500`) and `M` (`1000`) to make `400` and `900`.
29//! Given an integer, convert it to a roman numeral.
30//! 
31//! Example 1:
32//! ```plain
33//! Input: num = 3
34//! Output: "III"
35//! Explanation: 3 is represented as 3 ones.
36//! ```
37//! 
38//! Example 2:
39//! ```plain
40//! Input: num = 58
41//! Output: "LVIII"
42//! Explanation: L = 50, V = 5, III = 3.
43//! ```
44//! 
45//! Example 3:
46//! ```plain
47//! Input: num = 1994
48//! Output: "MCMXCIV"
49//! Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
50//! ```
51//! 
52//! Constraints:
53//! 
54//! - `1 $\leqslant$ num $\leqslant$ 3999`
55//! 
56//! Sources: <https://leetcode.com/problems/integer-to-roman/>
57
58////////////////////////////////////////////////////////////////////////////////
59
60/// Integer to Roman
61///
62/// # Arguments
63/// * `num` - input integer
64pub fn int_to_roman(num: i32) -> String {
65 convert(num)   
66}
67
68fn convert(n: i32)->String {
69    let mut num = n;
70    let roman_labels = vec!["I", "V", "X", "L", "C", "D", "M"];
71    let mut roman_index: usize = 0;
72    let mut output: Vec<&str> = vec![];
73    while num > 0 {
74        let last = num % 10;
75        num = num / 10;
76        if roman_index < roman_labels.len() {
77            match last {
78                1..=3 => {
79                    for _ in 0..last {
80                        output.insert(0, roman_labels[roman_index]);
81                    }
82                }
83                4 => {
84                    output.insert(0, roman_labels[roman_index]);
85                    output.insert(1, roman_labels[roman_index + 1]);
86                }
87                5..=8 => {
88                    output.insert(0, roman_labels[roman_index + 1]);
89                    for _ in 0..(last - 5) {
90                        output.insert(1, roman_labels[roman_index]);
91                    }
92                }
93                9 => {
94                    output.insert(0, roman_labels[roman_index]);
95                    output.insert(1, roman_labels[roman_index + 2]);
96                }
97                _ => (),
98            }
99
100            roman_index += 2;
101        } else {
102            for _ in 0..(last - 1) {
103                output.insert(0, roman_labels[roman_labels.len() - 1]);
104            }
105        }
106    }
107
108    output.as_slice().join("")
109}