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 ...
随机推荐
- 洛谷P1039 侦探推理(模拟)
侦探推理 题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情 ...
- 知识点:Mysql 索引原理完全手册(1)
知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) Mysql-索引原理完全手 ...
- Broadcom BCM94352z/DW1560驱动新姿势
转自:https://blog.daliansky.net/Broadcom-BCM94352z-DW1560-drive-new-posture.html Broadcom WiFi/BlueToo ...
- Jmeter发送邮件功能SMTP Sampler
介绍Jmeter的发送邮件功能,使用的Sampler是SMTP Sampler,详细说明每个配置项的功能 从上往下介绍需要用到的配置项: Server settings Server: 服务器地址 P ...
- 在Windows2008下安装SQL Server 2005无法启动服务的解决办法
在Windows2012下安装SQL Server 2005无法启动服务的解决办法 1.正常安装任一版本的SQL Server 2005. 2.安装到SqlServer服务的时候提示启动服务失败 此 ...
- row_number() over (partition by order by)的用法
原表为: 一.分区函数Partition By的与row_number()的用法 1.不分班按学生成绩排名 select *,row_number() over(order by Score desc ...
- JAVA体系学习-导向
一:当前学习内容 数值类型处理总结,字符类型处理总结,日期类型处理总结 spring 事务源码分析 spring源码系列 二:当前学习 主攻:并发编程->RPC原理->MQ原理->- ...
- !!常用HTML5代码
HTML5提供的新特性 2016-2-16 Web Socket 定义了一套API, 允许网页能够使用Web Socket协议来和远程主机进行双工通信. Web Storage 定义了一套API, 能 ...
- 利用pyinstaller生成exe之后,运行不能正常产生结果文件问题记录
https://segmentfault.com/q/1010000011284617/a-1020000011493026 在此链接已解决问题,现在在这里在详细记录一次 问题描述: 利用pychar ...
- js 字符串操作方法
1.字符串转换 你可以将任何类型的数据都转换为字符串,你可以用下面三种方法的任何一种: 1 2 var num= 19; // 19 var myStr = num.toString(); // &q ...