题目类型:
HashMap,字符串
beat:87%
题意:
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.罗马数字的写法:
![罗马数字的写法](https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1515832886&di=49daa22fa4c13b30fb3550fa54e1e342&src=http://p12.qhimg.com/t010e6ed1b8c9f8e2cf.png)罗马数字的规则:
- 罗马数字共有7个,
基本字符 | 相应的阿拉伯数字 |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
我的思路:
效率:52%步骤:
- 已知输入为1到3999,特殊数字为0 9 4 6 1
- 对从高位到低位的每一位数,如果为4/9就用左减,如果为5就用对应的5的表示方法,如果为1-3就用重复的三个一,如果为6-8就用五加一
- 例如3999,最高位为3,用重复的三个1000MMM,第二位为900,用1000-100:CM,第三位为90,用100-10:XC,第四位为9,用10-1,IX,拼接:MMMCMXCIX
public String intToRoman(int num) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "I");
map.put(5, "V");
map.put(10, "X");
map.put(50, "L");
map.put(100, "C");
map.put(500, "D");
map.put(1000, "M");
StringBuilder res = new StringBuilder();
int len = 1;
while (num / len >= 10) {
len *= 10;
}
while (num != 0) {
int cur = num/ len;
String roman = map.get(len);
if (cur == 4 || cur == 9) {
res.append(roman);
res.append(map.get((cur + 1) * len));
}
else{
if (cur >= 5) {
res.append(map.get(5 * len));
cur = cur - 5;
}
for(int i = 0; i < cur; i++){
res.append(roman);
}
}
num %= len;
len /= 10;
}
return res.toString();
}
方法二:不断减小的num
- 两个数组,分别存放阿拉伯数字及对应的罗马数字(包括0 9 5 4 1)
- for循环每一个阿拉伯数字,如果x - nums[i] >= 0,那么添加nums[i]对应的罗马数字,x减去nums[i]
- 循环直至x为0
class Solution {
//方法二:不断减
int[] nums = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] strings = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
public String intToRoman(int num) {
StringBuilder res = new StringBuilder();
for(int i = 0; i < nums.length; i++){
while (num >= nums[i]) {
res.append(strings[i]);
num -= nums[i];
}
}
return res.toString();
}
}