Skip to content

Commit 3a9aa9a

Browse files
committed
feat: add solutions to lc problem: No.0012
No.0012.Integer to Roman
1 parent 7939898 commit 3a9aa9a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

solution/0000-0099/0012.Integer to Roman/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,25 @@ function intToRoman(num: number): string {
192192
}
193193
```
194194

195+
### **C#**
196+
197+
```cs
198+
public class Solution {
199+
public string IntToRoman(int num) {
200+
List<string> cs = new List<string>{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
201+
List<int> vs = new List<int>{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
202+
StringBuilder ans = new StringBuilder();
203+
for (int i = 0; i < cs.Count; i++) {
204+
while (num >= vs[i]) {
205+
ans.Append(cs[i]);
206+
num -= vs[i];
207+
}
208+
}
209+
return ans.ToString();
210+
}
211+
}
212+
```
213+
195214
### **...**
196215

197216
```

solution/0000-0099/0012.Integer to Roman/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ M 1000</pre>
6262

6363
## Solutions
6464

65+
**Approach 1: Greedy**
66+
67+
We can list all possible symbols $cs$ and corresponding values $vs$ first, then enumerate the value $vs[i]$ from large to small, and use the symbol $cs[i]$ as much as possible each time until the number $num$ becomes $0$.
68+
69+
The time complexity is $O(m)$ and the space complexity is $O(m)$, where $m$ is the number of symbols.
70+
6571
<!-- tabs:start -->
6672

6773
### **Python3**
@@ -167,6 +173,25 @@ function intToRoman(num: number): string {
167173
}
168174
```
169175

176+
### **C#**
177+
178+
```cs
179+
public class Solution {
180+
public string IntToRoman(int num) {
181+
List<string> cs = new List<string>{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
182+
List<int> vs = new List<int>{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
183+
StringBuilder ans = new StringBuilder();
184+
for (int i = 0; i < cs.Count; i++) {
185+
while (num >= vs[i]) {
186+
ans.Append(cs[i]);
187+
num -= vs[i];
188+
}
189+
}
190+
return ans.ToString();
191+
}
192+
}
193+
```
194+
170195
### **...**
171196

172197
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public string IntToRoman(int num) {
3+
List<string> cs = new List<string>{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
4+
List<int> vs = new List<int>{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
5+
StringBuilder ans = new StringBuilder();
6+
for (int i = 0; i < cs.Count; i++) {
7+
while (num >= vs[i]) {
8+
ans.Append(cs[i]);
9+
num -= vs[i];
10+
}
11+
}
12+
return ans.ToString();
13+
}
14+
}

0 commit comments

Comments
 (0)