LeetCode--No.12--Integer to Roman

来源:互联网 发布:联想笔记本网络开关 编辑:程序博客网 时间:2024/06/07 10:58

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[] int_dict = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};        String[] roman_dict = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};        String roman = "";        int i = 0;        while(num > 0){            if(num >= int_dict[i]){                roman += roman_dict[i];                num -= int_dict[i];            }else{                i++;            }        }        return roman;    }}


0 0
原创粉丝点击