13 Roman to Integer
来源:互联网 发布:游戏优化 编辑:程序博客网 时间:2024/04/29 17:40
/*
注意:
基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;
V 和 X 左边的小数字只能用 Ⅰ;
L 和 C 左边的小数字只能用X;
D 和 M 左边的小数字只能用 C。
I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、 M(1000)
注意:
基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;
V 和 X 左边的小数字只能用 Ⅰ;
L 和 C 左边的小数字只能用X;
D 和 M 左边的小数字只能用 C。
I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、 M(1000)
*/
public class Solution {
public String intToRoman(int num) {
if(num==0) return null;
StringBuffer str = new StringBuffer();
//I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、 M(1000)
String[] romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
int len = romans.length;
while(num>0){
for(int i=0;i<len;++i){
if(num>=values[i]){
str.append(romans[i]);
num -= values[i];
break;
}
}
}
return str.toString();
}
}
0 0
- 13、Roman to Integer
- 13、Roman to Integer
- 13Roman to Integer
- #13 Roman to Integer
- 13Roman to Integer
- 13 Roman to Integer
- 13 Roman to Integer
- *13 Roman to Integer
- 13、Roman to Integer
- 13 Roman to Integer
- 【13】Roman to Integer
- 13 Roman to Integer
- 13-Roman to Integer
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- LeetCode_OJ【12】【13】Integer to Roman && Roman to Integer
- <LeetCode OJ> 13 / 12 Roman to Integer & Integer to Roman
- leetcode-13:Roman to Integer
- leetcode 13 Roman to Integer
- 深入浅出CChart 每日一课——快乐高四第十一课 不离不弃,利用CChartWnd实现多区域绘图
- Springmvc集成Shiro实现权限管理
- SSH之旅(一)——框架简介
- Keepalived原理与实战精讲
- TI Z-stack协议栈开发环境和工作流程
- 13 Roman to Integer
- hdoj2036
- BASE.CSS一份css初始化代码
- 没有为 COM 互操作注册程序集 请使用 regasm.exe /tlb 注册该程序集——解决办法
- mysql主从复制讲解
- 我的第一偏博客 怎么写啊 是这样吗???
- 一个Linux中用于监控的简易shell脚本
- LeetCode(238)Product of Array Except Self
- 字符集和字符编码(Charset & Encoding)