Leetcode_12_Integer to Roman
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42744649
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:
(1)题意为给定任意1—3999的整数,将其转化为罗马数字。
(2)该题和将罗马数字转化为整数类似,详见罗马数字转化为整数。
(3)由于技术有限,本文还是先运用“暴力破解”的思想,对于1-10、10-100、100-1000、1000-3999分别进行讨论,由于比较简单,这里就不累赘了,详情见下方代码。
(4)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public String intToRoman(int num) {
Map<Integer, String> maps = new HashMap<Integer, String>();
maps.put(1,"I");
maps.put(2,"II");
maps.put(3,"III");
maps.put(4,"IV");
maps.put(5,"V");
maps.put(6,"VI");
maps.put(7,"VII");
maps.put(8,"VIII");
maps.put(9,"IX");
maps.put(10,"X");
maps.put(20,"XX");
maps.put(30,"XXX");
maps.put(40,"XL");
maps.put(50,"L");
maps.put(60,"LX");
maps.put(70,"LXX");
maps.put(80,"LXXX");
maps.put(90,"XC");
maps.put(100,"C");
maps.put(200,"CC");
maps.put(300,"CCC");
maps.put(400,"CD");
maps.put(500,"D");
maps.put(600,"DC");
maps.put(700,"DCC");
maps.put(800,"DCCC");
maps.put(900,"CM");
maps.put(1000,"M");
maps.put(2000,"MM");
maps.put(3000,"MMM");
StringBuffer buffer = new StringBuffer();
if(num<=10){
return maps.get(num);
}else if(num>10 && num<100){
int index = num/10;
buffer.append(maps.get(index*10));
if(num-index*10>0){
buffer.append(maps.get(num-index*10));
}
return buffer.toString();
}else if(num>=100 && num<1000){
int hun = num/100;
buffer.append(maps.get(hun*100));
int te = (num-hun*100)/10;
if(te>0){
buffer.append(maps.get(te*10));
}
if(num-hun*100-te*10>0){
buffer.append(maps.get(num-hun*100-te*10));
}
return buffer.toString();
}else if(num>=1000 &&num<=3999){
int th = num/1000;
buffer.append(maps.get(th*1000));
int hun = (num-th*1000)/100;
if(hun>0){
buffer.append(maps.get(hun*100));
}
int te = (num-th*1000-hun*100)/10;
if(te>0){
buffer.append(maps.get(te*10));
}
if(num-th*1000-hun*100-te*10>0){
buffer.append(maps.get(num-th*1000-hun*100-te*10));
}
return buffer.toString();
}
return buffer.toString();
}
Leetcode_12_Integer to Roman的更多相关文章
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【leetcode】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- [Leetcode] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Integer to Roman -- LeetCode 012
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- No.013:Roman to Integer
问题: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- 20160227.CCPP体系详解(0037天)
程序片段(01):01.一对一模式.c+02.中介者模式.c+03.广播模式.c 内容概要:事件 ///01.一对一模式.c #include <stdio.h> #include < ...
- XCode使用技巧
XCode使用技巧 自动生成get.set方法 @property 用法 #import <Foundation/Foundation.h> @interface People : NSO ...
- 【SSH系列】spring中为什么要使用IOC
开篇前言 在前面的博文中,小编主要简单的介绍了spring的入门知识,随着学习的深入,我们知道spring最核心的两大技术,IOC和AOP,这两个技术也是spring最耀眼的地方,在后续的博文中小编将 ...
- python 函数运算先于单目运算
>>> def f(): >>> -f() - 初一看,-f()比较陌生的样子,细想,这是合理的
- [csdn markdown]使用摘记三 简便快捷的流程图
在线编写文字就可以实现复杂的流程图,再也不需要纠结了! 开始 操作流程 条件 结束 开始 st=>start: 开始 操作流程 st->op->cond 条件 cond=>co ...
- Java基础之枚举妙用
对于枚举,初学Java的时候可能我们就已经接触过了,但是在毕业前,其实一直都不知道真正工作里面枚举是怎么用的,枚举有什么用?接下来,博主就介绍枚举在实际工作中的一种使用场景,本文只适合初级的小菜鸟看哈 ...
- C++ 中const作用
一.对const与#define的特点及区别的理解 #define只是用来做文本替换的,#define常量的生命周期止于编译期,它存在于程序的代码段,在实际程序中它只是一个常数,一个命令中的参数,并没 ...
- 微信小程序之最简单的Demo设计使用
这个小Demo,代码量不多:导航样式.View.Text.点击.JS交互的使用,主要是理解每个后缀文件的功能,然后才能更好的使用开发.......(下面代码和源代码没差别,实在想要的请留言,谢谢... ...
- Android简易实战教程--第二十四话《画画板》
今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- tomcat集群的failover机制
集群要提供高可用性就必须要有某种机制去保证,常用的机制为failover(故障转移),简单说就是通过一定的heartbeat检测是否有故障,一旦故障发生备份节点则接管故障节点的工作. tomcat使用 ...