File tree 2 files changed +126
-0
lines changed
2 files changed +126
-0
lines changed Original file line number Diff line number Diff line change @@ -7,3 +7,4 @@ mod s0007_reverse_integer;
7
7
mod s0008_string_to_integer_atoi;
8
8
mod s0009_palindrome_number;
9
9
mod s0011_container_with_most_water;
10
+ mod s0012_integer_to_roman;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * [12] 整数转罗马数字
3
+ *
4
+ * 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
5
+ *
6
+ * 字符 数值
7
+ * I 1
8
+ * V 5
9
+ * X 10
10
+ * L 50
11
+ * C 100
12
+ * D 500
13
+ * M 1000
14
+ * 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
15
+ * 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
16
+ *
17
+ * I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
18
+ * X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
19
+ * C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
20
+ *
21
+ * 给你一个整数,将其转为罗马数字。
22
+ *
23
+ * 示例 1:
24
+ *
25
+ * 输入: num = 3
26
+ * 输出: "III"
27
+ * 示例 2:
28
+ *
29
+ * 输入: num = 4
30
+ * 输出: "IV"
31
+ * 示例 3:
32
+ *
33
+ * 输入: num = 9
34
+ * 输出: "IX"
35
+ * 示例 4:
36
+ *
37
+ * 输入: num = 58
38
+ * 输出: "LVIII"
39
+ * 解释: L = 50, V = 5, III = 3.
40
+ *
41
+ * 示例 5:
42
+ *
43
+ * 输入: num = 1994
44
+ * 输出: "MCMXCIV"
45
+ * 解释: M = 1000, CM = 900, XC = 90, IV = 4.
46
+ *
47
+ * 提示:
48
+ *
49
+ * 1 <= num <= 3999
50
+ *
51
+ */
52
+ pub struct Solution { }
53
+
54
+ // problem: https://leetcode.cn/problems/integer-to-roman/
55
+ // discuss: https://leetcode.cn/problems/integer-to-roman/discuss/?currentPage=1&orderBy=most_votes&query=
56
+
57
+ // submission codes start here
58
+
59
+ impl Solution {
60
+ pub fn int_to_roman ( num : i32 ) -> String {
61
+ let mut x = num;
62
+ let mut res = String :: new ( ) ;
63
+ while x > 0 {
64
+ if x >= 1000 {
65
+ x -= 1000 ;
66
+ res. push ( 'M' ) ;
67
+ } else if x >= 900 {
68
+ x -= 900 ;
69
+ res. push_str ( "CM" ) ;
70
+ } else if x >= 500 {
71
+ x -= 500 ;
72
+ res. push ( 'D' ) ;
73
+ } else if x >= 400 {
74
+ x -= 400 ;
75
+ res. push_str ( "CD" ) ;
76
+ } else if x >= 100 {
77
+ x -= 100 ;
78
+ res. push ( 'C' ) ;
79
+ } else if x >= 90 {
80
+ x -= 90 ;
81
+ res. push_str ( "XC" ) ;
82
+ } else if x >= 50 {
83
+ x -= 50 ;
84
+ res. push ( 'L' ) ;
85
+ } else if x >= 40 {
86
+ x -= 40 ;
87
+ res. push_str ( "XL" ) ;
88
+ } else if x >= 10 {
89
+ x -= 10 ;
90
+ res. push ( 'X' ) ;
91
+ } else if x >= 9 {
92
+ x -= 9 ;
93
+ res. push_str ( "IX" )
94
+ } else if x >= 5 {
95
+ x -= 5 ;
96
+ res. push ( 'V' ) ;
97
+ } else if x >= 4 {
98
+ x -= 4 ;
99
+ res. push_str ( "IV" ) ;
100
+ } else {
101
+ x -= 1 ;
102
+ res. push ( 'I' ) ;
103
+ }
104
+ }
105
+ res
106
+ }
107
+ }
108
+
109
+ // submission codes end
110
+
111
+ #[ cfg( test) ]
112
+ mod tests {
113
+ use crate :: solution;
114
+
115
+ use super :: * ;
116
+
117
+ #[ test]
118
+ fn test_12 ( ) {
119
+ assert_eq ! ( Solution :: int_to_roman( 1 ) , "I" ) ;
120
+ assert_eq ! ( Solution :: int_to_roman( 3 ) , "III" ) ;
121
+ assert_eq ! ( Solution :: int_to_roman( 4 ) , "IV" ) ;
122
+ assert_eq ! ( Solution :: int_to_roman( 1994 ) , "MCMXCIV" ) ;
123
+ assert_eq ! ( Solution :: int_to_roman( 58 ) , "LVIII" ) ;
124
+ }
125
+ }
You can’t perform that action at this time.
0 commit comments