Java [leetcode 12] Integer to Roman

题目描述:

Given an integer, convert it to a roman numeral.

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

解题思路:

这道题没什么技术含量,一个一个除10以及取模运算就行。

代码如下:

public class Solution {
    public String intToRoman(int num) {
		String Roman[][] = {
				{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
				{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
				{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
				{"", "M", "MM", "MMM"}
		};
		int remainder;
		int carry = num;
		int index = 0;
		String result = new String();
		while(carry != 0){
			remainder = carry % 10;
			result = Roman[index][remainder] + result;
			index++;
			carry = carry / 10;
		}
		return result;
	}
}
原文地址:https://www.cnblogs.com/zihaowang/p/4457001.html