本文是在学习中的总结,欢迎转载但请注明出处: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. Scheme r5rs letrec的用法

    说明,这是r5rs的用法. (letrec ((<variable> <init>) ...) <body>) 假设((<variable> <i ...

  2. Scala:类,对象和特征(接口)

    http://blog.csdn.net/pipisorry/article/details/52902609 Scala类和对象 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象 ...

  3. shell编程--基本格式,基本语法,运算符,expr,(()),$[]

    02/shell编程 Shell是用户与内核进行交互操作的一种接口,目前最流行的Shell称为bash Shell Shell也是一门编程语言."."号执行脚本时,会让脚本在调用者 ...

  4. LuaHotUpdate原理

    LuaHotUpdate原理(金庆的专栏)项目地址:https://github.com/asqbtcupid/lua_hotupdate只更新函数,不更新数据.主页上有个动画演示.限Windows平 ...

  5. 微信开发获取地理位置实例(java,非常详细,附工程源码)

    在本篇博客之前,博主已经写了4篇关于微信相关文章,其中三篇是本文基础: 1.微信开发之入门教程,该文章详细讲解了企业号体验号免费申请与一些必要的配置,以及如何调用微信接口. 2.微信开发之通过代理调试 ...

  6. RxJava(三) flatMap操作符用法详解

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51532776 本文出自:[余志强的博客] flatMap操作符的作用 ...

  7. Java进阶(四十六)简述ArrayList、Vector与LinkedList的异同点

    简述ArrayList.Vector与LinkedList的异同点   Collection类的继承图如下:   从图中可以看出,LinkedList与ArrayList.ArrayDeque这三者都 ...

  8. (NO.00005)iOS实现炸弹人游戏(九):游戏主角(二)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上篇介绍了游戏主角的初始化方法,下面我们一次来实现主角的其他方 ...

  9. 安装解压版本的MySQL,安装过程中的常见命令,检查windows系统错误日志的方式来检查MySQL启动错误,关于Fatal error: Can't open and lock privilege

     以端口 port = 3306 # 设置mysql的安装目录 basedir=D://Installed//mysql-5.6.26-winx64//mysql-5.6.26-winx64 # ...

  10. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...