热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

13.RomantoInteger(整数转罗马数字)

Givenaromannumeral,convertittoaninteger.Inputisguaranteedtobewithintherangefrom1to

Given a roman numeral, convert it to an integer.

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

题目大意:给定[1,3999]之间的某个整数,将其转为罗马数字。

解题思路:代码如下:(93ms,beats 75.93%)

public class Solution {
public String intToRoman(int num) {
int[] ints = new int[]{
1000,
900,500,400,100,
90,50,40,10,
9,5,4,1
};
String[] romans = new String[]{
"M",
"CM", "D", "CD", "C",
"XC", "L", "XL", "X",
"IX", "V", "IV", "I"
};
StringBuilder sb = new StringBuilder();
int index = 0;
while(num>0){
if(num-ints[index]>=0){
num-=ints[index];
sb.append(romans[index]);
}
else
index++;
}
return sb.toString();
}
}



推荐阅读
author-avatar
景雨韵文秀馨
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有