本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  3. 【leetcode】Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  4. [Leetcode] Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  5. 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 ...

  6. 【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 ...

  7. No.013:Roman to Integer

    问题: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from ...

  8. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  9. 【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 ...

随机推荐

  1. linux下安装apache(httpd-2.4.3版本)各种坑

    博主的linux是ubuntu 14.04.3. 在安装apache最新版httpd-2.4.3的时候遇到各种坑. 先提供安装apache httpd-2.4.3所需要的包,博主已经整理好,下载地址: ...

  2. Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted

    In this Document   Symptoms   Changes   Cause   Solution   References APPLIES TO: Oracle Application ...

  3. springMVC源码分析--RequestMappingHandlerAdapter(五)

    上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...

  4. Python 继承标准类时发生了什么

    定义标准类dict的一个子类c: >>> class c(dict): pass >>> y=c({1:2,3:4}) >>> y {1: 2, ...

  5. EBS中使用JAVA方式发送HTML格式邮件

    转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17174769 EBS中使用JAVA方式发送HTML格式邮件 一.开发工具:JDevel ...

  6. 集合框架之Queue接口

    Queue接口 在处理元素前用于保存元素的 collection.除了基本的 Collection 操作外,队列还提供其他的插入.提取和检查操作.每个方法都存在两种形式:一种抛出异常(操作失败时),另 ...

  7. iOS7 CookBook精彩瞬间(二)NSSet、通过Subscript访问类成员等

    1.NSSet的用法 ①NSSet是一种无序集合,基于hash实现,可以在线性时间复杂度内查找某个元素,如果要让元素有序,应当使用NSOrderedSet,要让集合可变,与NSArray类似,使用NS ...

  8. Android系统剪切板

    Android系统剪贴板 复制字符串到系统剪切板 /** * 复制文本到剪切板 * * @param text * @return */ @SuppressWarnings("depreca ...

  9. 用API创建用户

    DECLARE lc_user_name VARCHAR2(100) := 'PRAJ_TEST'; lc_user_password VARCHAR2(100) := 'Oracle123'; ld ...

  10. 从二进制数据流中构造GDAL可以读取的图像数据

    在很多时候,我们的图像数据往往都不是文件方式存储在磁盘上,而是可能从网络或者数据库中获取的是二进制的图像数据流.最简单的方式和最容易想到的方式就是将这个文件流保存到磁盘上形成一个文件,然后再使用GDA ...