算法練習題---羅馬數字轉int

鏈接:https://leetcode-cn.com/problems/roman-to-integer/submissions/java

題目:spring

羅馬數字包含如下七種字符: I, V, X, L,C,D 和 M。spa

字符 數值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 羅馬數字 2 寫作 II ,即爲兩個並列的 1。12 寫作 XII ,即爲 X + II 。 27 寫作  XXVII, 即爲 XX + V + II 。code

一般狀況下,羅馬數字中小的數字在大的數字的右邊。但也存在特例,例如 4 不寫作 IIII,而是 IV。數字 1 在數字 5 的左邊,所表示的數等於大數 5 減少數 1 獲得的數值 4 。一樣地,數字 9 表示爲 IX。這個特殊的規則只適用於如下六種狀況:blog

I 能夠放在 V (5) 和 X (10) 的左邊,來表示 4 和 9。
X 能夠放在 L (50) 和 C (100) 的左邊,來表示 40 和 90。 
C 能夠放在 D (500) 和 M (1000) 的左邊,來表示 400 和 900。
給定一個羅馬數字,將其轉換成整數。輸入確保在 1 到 3999 的範圍內。ip

示例 1:leetcode

輸入: "III"
輸出: 3
示例 2:get

輸入: "IV"
輸出: 4
示例 3:input

輸入: "IX"
輸出: 9
示例 4:it

輸入: "LVIII"
輸出: 58
解釋: L = 50, V= 5, III = 3.
示例 5:

輸入: "MCMXCIV"
輸出: 1994
解釋: M = 1000, CM = 900, XC = 90, IV = 4.

來源:力扣(LeetCode)
連接:https://leetcode-cn.com/problems/roman-to-integer

其中,方法2、三爲博主本人解答。方法一,目前爲查找的力扣中運行耗時相對較短的解法

解法一:

 
 
package com.zx.leetcode.roman2integer;

/**
* @Author JAY
* @Date 2019/6/16 14:25
* @Description 羅馬數字轉int
**/
public class SolutionV3 {

public static void main(String[] args) {
System.out.println(romanToInt("LVIII"));
}

public static int romanToInt(String s) {
char[] romanArray = s.toCharArray();
int result = 0;
int index = 0;
while (index < romanArray.length) {

switch (romanArray[index++]) {
case 'I':
if (index < romanArray.length && romanArray[index] == 'V') {
result += 4;
index++;
} else if (index < romanArray.length && romanArray[index] == 'X') {
result += 9;
index++;
} else {
result += 1;
}
break;
case 'V':
result += 5;
break;
case 'X':
if (index < romanArray.length && romanArray[index] == 'L') {
result += 40;
index++;
} else if (index < romanArray.length && romanArray[index] == 'C') {
result += 90;
index++;
} else {
result += 10;
}
break;
case 'L':
result += 50;
break;
case 'C':
if (index < romanArray.length && romanArray[index] == 'D') {
result += 400;
index++;
} else if (index < romanArray.length && romanArray[index] == 'M') {
result += 900;
index++;
} else {
result += 100;
}
break;
case 'D':
result += 500;
break;
case 'M':
result += 1000;
break;
default:
throw new IllegalArgumentException("The input isn't roman.");
}
}
return result;
}
}

解法二:

package com.zx.leetcode.roman2integer;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author JAY
 * @Date 2019/6/16 14:25
 * @Description 羅馬數字轉int
 **/
public class SolutionV2 {

    public static void main(String[] args) {
        System.out.println(romanToInt("LVIII"));
    }

    public static int romanToInt(String s) {
        if (null == s){
            return 0;
        }
        int sum = 0;

        TreeMap<String, Integer> romanNumAll = new TreeMap<String, Integer>();
        romanNumAll.put("I",1);
        romanNumAll.put("V",5);
        romanNumAll.put("X",10);
        romanNumAll.put("L",50);
        romanNumAll.put("C",100);
        romanNumAll.put("D",500);
        romanNumAll.put("M",1000);

        Map<String, Integer> romanNumSort = new HashMap<>(7);
        romanNumSort.put("I",1);
        romanNumSort.put("V",2);
        romanNumSort.put("X",3);
        romanNumSort.put("L",4);
        romanNumSort.put("C",5);
        romanNumSort.put("D",6);
        romanNumSort.put("M",7);

        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++){
            String charI = String.valueOf(chars[i]);
            if (i + 1 >= chars.length){
                sum = sum + romanNumAll.get(charI);
                break;
            }
            String charI1 = String.valueOf(chars[i + 1]);
            if (romanNumSort.get(charI) >= romanNumSort.get(charI1)){
                sum = sum + romanNumAll.get(charI);
            } else {
              sum = sum + (romanNumAll.get(charI1) - romanNumAll.get(charI));
              i = i + 1;
            }
        }
        if (sum > 3999){
            return 3999;
        }
        return sum;
    }
}

解法三:

package com.zx.leetcode.roman2integer;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author JAY
 * @Date 2019/6/16 14:25
 * @Description 羅馬數字轉int
 **/
public class Solution {

    public static void main(String[] args) {
        System.out.println(romanToInt("IX"));
    }

    public static int romanToInt(String s) {
        if (s == null){
            return 0;
        }
        Map<String, Integer> romanNumAll = new HashMap<>(7);
        romanNumAll.put("I",1);
        romanNumAll.put("V",5);
        romanNumAll.put("X",10);
        romanNumAll.put("L",50);
        romanNumAll.put("C",100);
        romanNumAll.put("D",500);
        romanNumAll.put("M",1000);

        Map<String, Integer> romanNum = new HashMap<>(6);
        romanNum.put("IV",4);
        romanNum.put("IX",9);
        romanNum.put("XL",40);
        romanNum.put("XC",90);
        romanNum.put("CD",400);
        romanNum.put("CM",900);
        int sum = 0;

        for (Map.Entry<String , Integer> entry : romanNum.entrySet()){
            if (s.contains(entry.getKey())){
                sum = sum + entry.getValue();
                s = s.replace(entry.getKey(),"_");
            }
        }
        String[] split = s.split("_");
        if (split != null && split.length > 0){
            for (String s1 : split) {
                char[] chars = s1.toCharArray();
                for (char aChar : chars) {
                    sum = sum + romanNumAll.get(String.valueOf(aChar));
                }
            }
        }
        if (sum > 3999){
            return 3999;
        }
        return sum;
    }
}
相關文章
相關標籤/搜索