本文是在学习中的总结,欢迎转载但请注明出处: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. 独立开发一个云(PaaS)的核心要素, Go, Go, Go!!!

    最近一年的工作,有很大的比重在做云平台的事情,简单来说,就是为公司内用户提供一个PaaS,用户可以在我们的云平台上方便的将单机服务程序扩展为多实例程序,以平台服务化的方式对外提供.在这里简单分享一下. ...

  2. JAVA面向对象-----包机制

    JAVA面向对象-–包机制 问题: 当定义了多个类的时候,可能会发生类名的重复问题. 在java中采用包机制处理开发者定义的类名冲突问题. 怎么使用java的包机制呢? 1.使用package 关键字 ...

  3. Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)

    demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...

  4. Android Multimedia框架总结(十三)CodeC部分之OpenMAX框架初识及接口与适配层实现

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52629598 前言:上篇中介绍O ...

  5. HDFS:NameNode、DataNode、SecondaryNameNode

    可以一句话描述 HDFS:把客户端的大文件存放在很多节点的数据块中. HDFS设计原则: 1,文件以块(block)方式存储: 2,通过副本机制提高可靠度和读取吞吐量: 3,每个区块至少分到三台Dat ...

  6. Retrofit 2.0 超能实践(四),完成大文件断点下载

    作者:码小白 文/CSDN 博客 本文出自:http://blog.csdn.net/sk719887916/article/details/51988507 码小白 通过前几篇系统的介绍和综合运用, ...

  7. FFmpeg与libx264接口源代码简单分析

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  8. javascript中的XML

    IE下创建DOM并载入XML var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); xmldoc.load(url); //载入X ...

  9. API创建/更新员工薪水

    DECLARE lb_inv_next_sal_date_warning BOOLEAN; lb_proposed_salary_warning BOOLEAN; lb_approved_warnin ...

  10. 04 AutoCompleteTextView

    作用:输入部分文字跳处下拉框列出相应的条目 <pre name="code" class="html"> <!-- 当文本框出现两个字符才开始 ...