您的位置:首页 > 其它

Leetcode Integer to Roman

2016-07-19 04:56 309 查看
Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

Difficulty: Medium

public class Solution {
public String intToRoman(int num) {
String ans = "";
char[] c = new char[7];
c[0] = 'I';
c[1] = 'V';
c[2] = 'X';
c[3] = 'L';
c[4] = 'C';
c[5] = 'D';
c[6] = 'M';
for(int i = 0; i < 4; i++){
int temp = num%10;
if(temp <= 3 && temp >=1){
for(int j = 0; j < temp; j++){
ans = ans + c[i*2];
}
}
else if(temp == 4){
ans = ans + c[i*2 + 1];
ans = ans + c[i*2];
}
else if(temp >= 5 && temp <= 8){
for(int j = temp; j >= 6; j--){
ans = ans + c[i*2];
}
ans = ans + c[i*2 + 1];
}
else if(temp == 9){
ans = ans + c[i*2 + 2];
ans = ans + c[i*2];
}
num /= 10;
}
return (new StringBuilder(ans)).reverse().toString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: