12. Integer to Roman (JAVA)
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: 3
Output: "III"
Example 2:
Input: 4
Output: "IV"
Example 3:
Input: 9
Output: "IX"
Example 4:
Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 5:
Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
class Solution {
public String intToRoman(int num) {
int len;
String str="";
int[] val = {1000,500,100,50,10,5,1};
char[] sym = {'M','D','C','L','X','V','I'};
for(int i = 0; i < val.length; i+=2){
len = num/val[i];
if(len <=3 ){ //0-3
for(int j = 0; j < len; j++){
str += sym[i];
}
}
else if(len == 4){ //
str = str + sym[i] + sym[i-1];
}
else if(len <= 8){ //5-8
str += sym[i-1];
for(int j = 5; j < len; j++){
str += sym[i];
}
}
else{ //
str = str + sym[i] + sym[i-2];
}
num %= val[i];
}
return str;
}
}
解题思路:
使用两个对应的数组,以减少代码的重复量
12. Integer to Roman (JAVA)的更多相关文章
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】12. Integer to Roman (2 solutions)
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [LeetCode] 12. Integer to Roman ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- 小程序sitemap配置
这几天在官网文档中发现小程序也支持索引了链接,就是是在根目录下新建sitemap.json 文件. { "rules": [ { "action": " ...
- maven教程全攻略
maven教程全攻略 我们在开发项目的过程中,会使用一些开源框架.第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要添加到项目中,所有这些相关 ...
- Python爬虫的步骤和工具
#四个步骤 1.查看crawl内容的源码格式 crawl的内容可以是 url(链接),文字,图片,视频 2.请求网页源码 (可能要设置)代理,限速,cookie 3.匹配 用正则表达 ...
- C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点
#region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...
- iOS开发- 相机(摄像头)获取到的图片自动旋转90度解决办法
http://blog.csdn.net/hitwhylz/article/details/39518463
- pyqt5 -——菜单和工具栏
一. 状态栏 # -*- coding: utf-8 -*-# @Time : 2018/12/22 12:37# @Author : Bo# @Email : mat_wu@163.com# @Fi ...
- Ubuntu16.04 导入tensorflow报错
错误1:Traceback (most recent call last): File "/home/lwc/anaconda3/lib/python3.6/site-packages/t ...
- 在linux上安装docker
我的linux系统是阿里云服务器,是centos版本的. 前置条件 64-bit 系统 kernel 3.10+ 用uname -r命令检查内核版本,返回的值大于3.10即可. 用sudo wget ...
- Vue note
1.npm run build 时,font:xx/xx "xxxx" 这种样式打包后会无效,只能写成font-size:xxx; line-height:xxx; font-fa ...
- EFM32之GPIO
配置时钟: void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable) CMU_ClockEnable(cmuClock_HFPER, tru ...