LeetCode 13. Roman to Integer---Java

题目:

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:
Input: “III”
Output: 3

Example 2:
Input: “IV”
Output: 4

Example 3:
Input: “IX”
Output: 9

Example 4:
Input: “LVIII”
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:
Input: “MCMXCIV”
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

解答:

首先想到的是用if-else进行判断,分情况进行计算。

class Solution {
    public int romanToInt(String s) {
        char[] arr=s.toCharArray();
        int sum=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]=='I') sum+=1;
            if(arr[i]=='V'){
                if(i!=0&&arr[i-1]=='I') sum+=3;
                else sum+=5;
            }
            if(arr[i]=='X'){
                if(i!=0&&arr[i-1]=='I') sum+=8;
                else sum+=10;
            }
            if(arr[i]=='L'){
                if(i!=0&&arr[i-1]=='X') sum+=30;
                else sum+=50;
            }
            if(arr[i]=='C'){
                if(i!=0&&arr[i-1]=='X') sum+=80;
                else sum+=100;
            }
            if(arr[i]=='D'){
                if(i!=0&&arr[i-1]=='C') sum+=300;
                else sum+=500;
            }
            if(arr[i]=='M'){
                if(i!=0&&arr[i-1]=='C') sum+=800;
                else sum+=1000;
            }
        }
        return sum;
    }
}

但上述方法过于繁琐,后来想到可以 逆序遍历字符串中的每一个字符,如果小的数在大的数前面,就需要拿大的数减去小的数,否则相加。

class Solution {
    public int romanToInt(String s) {
        //先根据题中的字符表,将字符映射为数字
        Map<Character,Integer> map=new HashMap<>();
        map.put('I',1);
        map.put('V',5);
        map.put('X',10);
        map.put('L',50);
        map.put('C',100);
        map.put('D',500);
        map.put('M',1000);
        int sum=map.get(s.charAt(s.length()-1));
        for(int i=s.length()-2;i>=0;i--){
            if(map.get(s.charAt(i))>=map.get(s.charAt(i+1))){
                sum+=map.get(s.charAt(i));
            }else{
                sum=sum-map.get(s.charAt(i));
            }
        }
        return sum;
    }
}

第一次写上述代码时,产生了字符串下标越界的报错。因为当遍历到最后一个字母时,没有第i+1个字母与其进行比较。因此去除了最后一个字母的索引。
LeetCode 13. Roman to Integer---Java_第1张图片

补充:

依次取出字符串中每个字符:

String str="1234567";
for(int i=0;i<str.length();i++){
    System.out.println(str.charAt(i));//str.charAt(i)为取字符串的第i个字符
}

将Java字符串转化为数组:

String str="1234567";
char[] arr=str.toCharArray(); //转换为char数组
for(int i=0;i<ar.length;i++){
    System.out.println(arr[i]);输出1 2 3 4 5 6
}

或者采用Java Split()方法
split()方法根据匹配给定的正则表达式来拆分字符串

//字符串转数组,java.lang.String
String str="0,1,2,3,4,5"
String[] arr=str.split(",");//String数组,用,进行分割
System.out.println(Arrays.toString(arr));//[0,1,2,3,4,5]

你可能感兴趣的:(LeetCode)