12. Integer to Roman Medium

@problem@discussion
#Hash Table#Math#String



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


Back
© 2025 bowen.ge All Rights Reserved.