本文主要是介绍leetcode:Integer to Roman 【Java】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、问题描述
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
二、问题分析
无
三、算法代码
public class Solution {public String intToRoman(int num) {int radix[] = {1000, 900, 500, 400, 100, 90,50, 40, 10, 9, 5, 4, 1};String symbol[] = {"M", "CM", "D", "CD", "C", "XC","L", "XL", "X", "IX", "V", "IV", "I"};String roman = "";for (int i = 0; num > 0; ++i) {int count = num / radix[i];num %= radix[i];for (; count > 0; --count) roman += symbol[i];
}return roman;}
}
这篇关于leetcode:Integer to Roman 【Java】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!