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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan 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 ...
随机推荐
- snmp服务中snmpwalk命令常用方法总结
在日常监控中,经常会用到snmp服务,而snmpwalk命令则是测试系统各种信息最有效的方法,命令格式: snmpwalk -c SNMP读密码 -v 1或2(代表SNMP版本) 交换机或路由器IP ...
- nice team小组出山啦!(第二次会议)
为什么niceteam小组一个月没有更新博客?因为我们正在埋头苦干.是的过去的一个月我们的小组成员正沉浸在知识的海洋中,今天是我们出山的好日子,特地记录一下我们的工作进度. 在项目经理的安排下,我们小 ...
- 无外接键盘安装 raspberry pi 3B+ 安装系统
从官网介绍看,当前raspbian和以前大家的记录略有不同,老的博客资料基本都是介绍下载raspbian,但现在raspbian已经不再维护镜像, raspbian系统开始由官方 pi foundat ...
- yum搭建 Lamp环境
yum搭建Lamp yum install -y httpd yum install -y nano rpm 安装 Php7 相应的 yum源 rpm -Uvh https://dl.fedorapr ...
- 使用Git Bash从Git上下载代码到本地以及上传代码到码云Git
前提是在码云上已经新建一个空的项目,可参考:https://www.cnblogs.com/babysbreath/p/9170455.html 1.新建一个目录,存放下载下来的项目,我在D盘新建了一 ...
- windows10下 MySQL5.7.18版本安装过程及遇到的问题
windows10下 MySQL5.7.18版本安装过程及遇到的问题 mysql-5.7.18-winx64 安装 1.解压 此次将MySQL装在H盘,依个人喜 ...
- Java垃圾回收(整理)
Java垃圾回收 Garbage Collection:GC: 什么样的对象才是垃圾?怎样判断一个对象引用是不是垃圾? 垃圾回收算法:Mark-Sweep(标记-清除)算法,Copying(复制)算法 ...
- MYSQL数据库中中文乱码问题
show variables like 'character%'; set character_set_database=gbk; 把记事本中的代码引入到mysql数据库中:source +addre ...
- 13. nginx,lvs之一
摘要: 1.详细描述常见nginx常用模块和模块的使用示例 2.简述Linux集群类型.系统扩展方式及调度方法 3.简述lvs四种集群有点及使用场景 4.描述LVS-NAT.LVS-DR的工作原理并实 ...
- python docker 多进程提供 稳定tensorflow gpu 线上服务
尝试了太多的python多进程的服务,在tensorflow 的线上GPU服务中总是不理想.tensorlfow serving docker服务这些也有些不便. 今天抽空给大家分享一个成功的经验.失 ...