13. Roman to Integer and Integer to Roman

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Leetcode 12, 13(需要考虑输入不合法的情况,比如“VV”或者“IIII”都是不合法的输入,要输出错误信息): if (s.indexOf(II) .. throw new A....

static void checkEligibilty(int stuage, int stuweight){ 
      if(stuage<12 && stuweight<40) {
         throw new ArithmeticException("Student is not eligible for registration"); 
      }
      else {
         System.out.println("Student Entry is Valid!!"); 
      }
   }

import java.util.HashMap;


public class Roman {
	public static int romanToInteger(String s) throws Exception {
		if (s.length() == 0 || s == null) {
		        return 0;
		}
		HashMap<Character, Integer> dict = new HashMap<Character, Integer>();
		dict.put('I', 1);
		dict.put('V', 5);
		dict.put('X', 10);
		dict.put('L', 50);
		dict.put('C', 100);
		dict.put('D', 500);
		dict.put('M', 1000);
		int len = s.length();
		int result = dict.get(s.charAt(len - 1));
		// check illegal input
		int sameCount = 1;
		for (int i = 0; i < len; i++) {
			if (!dict.containsKey(s.charAt(i))) {
				throw new Exception("Illegal input: invalid character " + s.charAt(i));
			}
			if (i < len - 1 && dict.get(s.charAt(i)) < dict.get(s.charAt(i + 1))) {
				int diff = dict.get(s.charAt(i + 1)) - dict.get(s.charAt(i));
				if (diff != 4 && diff != 9 && diff != 40 && diff != 90 && diff != 400 && diff != 900) {
					throw new Exception("Illegal input: invalid sequence " + s.charAt(i) + s.charAt(i + 1));
				}
			}
			if (i > 0 && s.charAt(i) == s.charAt(i - 1)) {
				sameCount++;
			} else {
				sameCount = 1;
			}
			if (sameCount > 1) {
				if (s.charAt(i) == 'V' || s.charAt(i) == 'L' || s.charAt(i) == 'D') {
					throw new Exception("Illegal input: 2 or more consecutive " + s.charAt(i));
				}
			}
			if (sameCount > 3) {
				throw new Exception("Illegal input: 4 or more consecutive " + s.charAt(i));
			}
		}
		
		for (int i = 0; i < len - 1; i++) {
		    if (dict.get(s.charAt(i)) >= dict.get(s.charAt(i + 1))) {
		        result += dict.get(s.charAt(i));
		    } else {
		        result -= dict.get(s.charAt(i));
		    }
		}
		return result;
	}
	
	public static String integerToRoman(int num) {
		String[] sdict = {"M", "CM", "D", "CD", 
                "C", "XC", "L", "XL", "X", 
                "IX", "V", "IV", "I"};
		int[] idict = {1000, 900, 500, 400, 100,
              90, 50, 40, 10, 9, 5, 4, 1};
		String result = "";
		while (num > 0) {
		  int i;
		  for (i = 0; i < idict.length; i++) {
		      if (num >= idict[i]) {
		          result = result + sdict[i];
		          break;
		      }
		  }
		  num -= idict[i];
		}
		return result;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(romanToInteger("XCV"));
		System.out.print(integerToRoman(95));
	}
	
}

  

 
如今我们最常见的罗马数字就是钟表的表盘符号:Ⅰ,Ⅱ,Ⅲ,Ⅳ(IIII),Ⅴ,Ⅵ,Ⅶ,Ⅷ,Ⅸ,Ⅹ,Ⅺ,Ⅻ……
对应阿拉伯数字(就是现在国际通用的数字),就是1,2,3,4,5,6,7,8,9,10,11,12。(注:阿拉伯数字其实是古代印度人发明的,后来由阿拉伯人传入欧洲,被欧洲人误称为阿拉伯数字。)
 
 
基本字符
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。
 
而这道题好就好在没有让我们来验证输入字符串是不是罗马数字,这样省掉不少功夫。我们需要用到map数据结构,来将罗马数字的字母转化为对应的整数值,因为输入的一定是罗马数字,那么我们只要考虑两种情况即可:
第一,如果当前数字是最后一个数字,或者之后的数字比它小的话,则加上当前数字
第二,其他情况则减去这个数字
 
代码如下:
 
解法一:

从右向左,preVal, curVal

1. curVal >= preVal:  res+=curVal

2. curVal < preVal: res -= curVal

public class Solution {
    public static int romanToInt(String s) {
        int res = 0;
        int preVal = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            int curVal = 0;
            switch (c) {
            case 'I':
                curVal = 1;
                break;
            case 'V':
                curVal = 5;
                break;
            case 'X':
                curVal = 10;
                break;
            case 'L':
                curVal = 50;
                break;
            case 'C':
                curVal = 100;
                break;
            case 'D':
                curVal = 500;
                break;
            case 'M':
                curVal = 1000;
                break;
            }
            if (curVal >= preVal) res += curVal;
            else res -= curVal;
            preVal = curVal;
        }
        return res;
    }
}

count every Symbol and add its value to the sum, and minus the extra part of special cases.

public int romanToInt(String s) {
     int sum=0;
    if(s.indexOf("IV")!=-1){sum-=2;}
    if(s.indexOf("IX")!=-1){sum-=2;}
    if(s.indexOf("XL")!=-1){sum-=20;}
    if(s.indexOf("XC")!=-1){sum-=20;}
    if(s.indexOf("CD")!=-1){sum-=200;}
    if(s.indexOf("CM")!=-1){sum-=200;}
    
    char c[]=s.toCharArray();
    int count=0;
    
   for(;count<=s.length()-1;count++){
       if(c[count]=='M') sum+=1000;
       if(c[count]=='D') sum+=500;
       if(c[count]=='C') sum+=100;
       if(c[count]=='L') sum+=50;
       if(c[count]=='X') sum+=10;
       if(c[count]=='V') sum+=5;
       if(c[count]=='I') sum+=1;
       
   }
   
   return sum;
    
}

  

原文地址:https://www.cnblogs.com/apanda009/p/7392455.html