Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

12. Integer to Roman

Posted on 2020-06-13 | Edited on 2021-01-22

LeetCode

Approach: Greedy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public String intToRoman(int num) {
int[] vals = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
String[] nums = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

StringBuilder result = new StringBuilder();
for (int i = 0; i < vals.length; i++) {
while (num >= vals[i]) {
result.append(nums[i]);
num -= vals[i];
}
}
return result.toString();
}
}

<1…160161162…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%