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 ...
随机推荐
- iOS 中的block异常
转自:iOS 知识小集 我们在调用block时,如果这个block为nil,则程序会崩溃,报类似于EXC_BAD_ACCESS(code=1, address=0xc)异常[32位下的结果,如果是64 ...
- 高端技巧:如何使用#define定义变量
Introduction 想在源文件中定义一个跟行号有关的变量,每次都手动输入实在是太慢了,本文介绍如何使用宏定义来定义与行号有关的变量. 例如:我们想在源代码的第10行定义A_10这样的一个整形变量 ...
- Swift按照数组元素出现的次数及大小排序
要求如下: 1.已知一个数组,按照单个元素在数组中出现的次数作为重新排序的依据,个数多的排在前面 2.相同个数时候,元素值大的排前面 例子: [1, 2, 2, 3, 5, 5] 经过计算得到的结果是 ...
- Redis 学习笔记2:redis.conf配置文件详解
Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 参数说明: 参数说明 redis.conf 配置项说明如下: 1. Redis默认不是以守护进程的方式运行,可以通 ...
- iOS下JS与OC互相调用(八)--Cordova详解+实战
扯两句,可以跳过 由于项目中Cordova相关功能一直是同事在负责,所以也没有仔细的去探究Cordova到底是怎么使用的,又是如何实现JS 与 OC 的交互.所以我基本上是从零开始研究和学习Cordo ...
- Android自定义View(一、初体验自定义TextView)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客] 目录: 继承View重写onDraw方法 自 ...
- XMPP系列(六)---创建群组
最近公司项目需要,要做一个自己的IMSDK,顺便先把之前没有记录的群聊功能记录一下. 先上资料,查看XMPP群聊相关的资料,可以去这里看协议:XEP-0045 . 创建群组 XMPP 框架里有一个类X ...
- Android Multimedia框架总结(一)MediaPlayer介绍之状态图及生命周期
请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52349221 前言:从本篇开始,将进入Multimedia框架,包含 ...
- 【移动开发】plurals
不同的语言对数量的语法规定有不同的规则.在英语里面,例如,1是特例.我们会直接写1book,而针对一个以上的我们会在book后加复数形式.这种区别对单数和复数来说是很普遍的,但是其他的语言做了更好的区 ...
- Centos7安装Tair及配置测试
系统环境 Centos7 64位 外网ip 182.254.145.66 内网ip 10.105.23.114 安装位置 /usr/local/tair Tair介绍 参见官网 安装 想了半天,我还是 ...