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 ...
随机推荐
- POSIX 消息队列相关问题
一.查看和删除消息队列要想看到创建的posix消息队列,需要在root用户下执行以下操作:# mkdir /dev/mqueue# mount -t mqueue none /dev/mqueue删除 ...
- 无法启动postgresql的错误
chown postgres /etc/ssl/private/ssl-cert-snakeoil.key chgrp postgres /etc/ssl/private/ssl-cert-snake ...
- leetcode 之 Single Number II
问题来源:Single Number II 问题描述:给定一个整数数组,除了一个整数出现一次之外,其余的每一个整数均出现三次,请找出这个出现一次的整数. 大家可能很熟悉另一个题目(Single Num ...
- Apache Curator入门实战
Apache Curator入门实战 Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeep ...
- Device Mapper 代码分析
Device Mapper(DM)是Linux 2.6全面引入的块设备新构架,通过DM可以灵活地管理系统中所有的真实或虚拟的块设备. DM以块设备的形式注册到Linux内核中,凡是挂载(或者说&quo ...
- Swift中的可选协议和方法的历史渊源
@objc protocol Transaction { func commit() -> Bool optional func isComplete() -> Bool } 以上协议被标 ...
- Android开发使用Java8新特性
Android 支持所有 Java 7 语言功能,以及一部分 Java 8 语言功能(具体因平台版本而异).本文介绍您可以使用的新语言功能.如何正确配置项目以使用这些功能,以及您可能遇到的任何已知问题 ...
- Mybatis源码分析--返回值ResultType和ResultMap
这一篇博客我们来介绍一下Mybatis执行sql语句返回的结果值的到实体对象的映射机制.首先ResultType和ResultMap的使用方式是不同的. ResultType的使用方式: result ...
- Hibernate通过SQL查询常量时只能返回第一个字符的解决方法
在Hibernate中如果通过 [java] view plaincopy session.createSQLQuery("select '合计' as name from dual&quo ...
- Android Demo---实现从底部弹出窗口
在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选 ...