Integer to Roman LeetCode Java
Posted 昵称真难想
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Integer to Roman LeetCode Java相关的知识,希望对你有一定的参考价值。
描述
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析
无
代码
1 public class IntegerToRoman { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 int num = 187; 6 System.out.println(intToRoman(num)); 7 } 8 9 public static String intToRoman(int num) { 10 int radix[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; 11 String symbol[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; 12 String roman = ""; 13 for (int i = 0; num > 0; ++i) { 14 int count = num / radix[i]; 15 num %= radix[i]; 16 for (; count > 0; --count) 17 roman += symbol[i]; 18 } 19 return roman; 20 } 21 22 }
以上是关于Integer to Roman LeetCode Java的主要内容,如果未能解决你的问题,请参考以下文章