现在的位置: 首页 > 综合 > 正文

[LeetCode] Roman to Integer || Integer to Roman (JAVA)

2018年03月31日 ⁄ 综合 ⁄ 共 2603字 ⁄ 字号 评论关闭

罗马字母介绍:(来源百度百科:http://baike.baidu.com/view/42061.htm?fr=aladdin)

基本字符
I
V
X
L
C
D
M
相应的阿拉伯数字表示为
1
5
10
50
100
500
1000
  1. 相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
  2. 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
  3. 小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
  4. 正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
  5. 在一个数的上面画一条横线,表示这个数扩大1000倍。
有几条须注意掌握:
  1. 基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个。
  2. 不能把基本数字V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目,只能使用一个。
  3. V 和X 左边的小数字只能用Ⅰ。
  4. L 和C 左边的小数字只能用X。
  5. D 和M 左边的小数字只能用C。
  • 个位数举例
    Ⅰ,1 】Ⅱ,2】 Ⅲ,3】 Ⅳ,4 】Ⅴ,5 】Ⅵ,6】Ⅶ,7】 Ⅷ,8 】Ⅸ,9 】
  • 十位数举例
    Ⅹ,10】 Ⅺ,11 】Ⅻ,12】 XIII,13】 XIV,14】 XV,15 】XVI,16 】XVII,17 】XVIII,18】 XIX,19】 XX,20】 XXI,21 】XXII,22 】XXIX,29】 XXX,30】 XXXIV,34】 XXXV,35 】XXXIX,39】 XL,40】 L,50 】LI,51】 LV,55】
    LX,60】 LXV,65】 LXXX,80】 XC,90 】XCIII,93】 XCV,95 】XCVIII,98】 XCIX,99 】
  • 百位数举例
    C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】
  • 千位数举例
    M,1000】 MC,1100 】MCD,1400 】MD,1500 】MDC,1600 】MDCLXVI,1666】 MDCCCLXXXVIII,1888 】MDCCCXCIX,1899 】MCM,1900 】MCMLXXVI,1976】 MCMLXXXIV,1984】 MCMXC,1990 】MM,2000 】MMMCMXCIX,3999】


I、Roman to Integer

public class Solution {
    public int romanToInt(String s) {
        int result = 0;
        Map<Character,Integer> roman = new HashMap<Character,Integer>();//定义map映射每一个罗马字母代表的整数;
        roman.put('I',1);
        roman.put('V',5);
        roman.put('X',10);
        roman.put('L',50);
        roman.put('C',100);
        roman.put('D',500);
        roman.put('M',1000);
        
        for(int i=s.length()-1;i>=0;i--){
            if(i==s.length()-1){
                result = roman.get(s.charAt(i));//获取罗马数字的第一个数字
                continue;                       //取得第一个罗马数字后,跳出本次循环,避免后面判断语句(i+1)越界
            }
            if(roman.get(s.charAt(i))<roman.get(s.charAt(i+1))){
                result -= roman.get(s.charAt(i));
            }else{
                result += roman.get(s.charAt(i));
            }
        }
        return result;
    }
}

II、Integer to Roman

public class Solution {
    public String intToRoman(int num) {
        Map<Integer,String> roman = new HashMap<Integer,String>();
		roman.put(1,"I");
		roman.put(5,"V");
		roman.put(10,"X");
		roman.put(50,"L");
		roman.put(100,"C");
		roman.put(500,"D");
		roman.put(1000,"M");
		
		roman.put(2,"II");
		roman.put(3,"III");
		roman.put(4,"IV");
		roman.put(6,"VI");
		roman.put(7,"VII");
		roman.put(8,"VIII");
		roman.put(9,"IX");
		
		roman.put(20,"XX");
		roman.put(30,"XXX");
		roman.put(40,"XL");
		roman.put(60,"LX");
		roman.put(70,"LXX");
		roman.put(80,"LXXX");
		roman.put(90,"XC");
		
		roman.put(200,"CC");
		roman.put(300,"CCC");
		roman.put(400,"CD");
		roman.put(600,"DC");
		roman.put(700,"DCC");
		roman.put(800,"DCCC");
		roman.put(900,"CM");
		
		roman.put(2000,"MM");
		roman.put(3000,"MMM");
		
		String value = Integer.toString(num);
		String result = "";
		int key = 1;
		for(int i=value.length()-1;i>=0;i--){
			if(value.charAt(i)=='0'){
				key = key*10;
				continue;
			}
			if(i==value.length()-1){
				result = roman.get(Integer.parseInt(String.valueOf(value.charAt(i))));
				key = key*10;
				continue;
			}
			result = roman.get(Integer.parseInt(String.valueOf(value.charAt(i)))*key)+result;
			key = key*10;
		}
		return result;
    }
}

抱歉!评论已关闭.