p12

LeetCode 12 Integer to Roman 题解

1.题目:

Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.

题意:

将整形转换为罗马字符串输出

2.解题思路:

跟转换进制一样的思路

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 

public class Solution {
public String intToRoman(int num) {
String ans;
String[] A={"","M","MM","MMM"};//1000
String[] B={"","C","CC","CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};//100
String[] C={"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};//10
String[] D={"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};//1
int a=num/1000;
int b=num/100-10*a;
int d=num%10;
int c=num%100/10;

return A[a]+B[b]+C[c]+D[d];
}
}


4.一些总结: