Programming

Leetcode-13. Roman to Integer

Leetcode-13. Roman to Integer

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, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 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.

Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.

Example 2:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 3:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

<解題>

  1. 先定義每一個字元對應的Value -> HashMap
  2. 值大的字母擺在越前面,故從前面往後找,當下一位字母的值比前位大時,要用減法
  3. 減法計算:記得減去前一位的2倍(因為本來被加過一次了,所以要扣回)
1. 對應字元->直接用字元比較好

class Solution {
    public int romanToInt(String s) {
        int result = 0;
        if(s != ""){
            HashMap<Character,Integer> map = new HashMap<Character,Integer>();
            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);

            for(int i = 0 ; i<s.length() ; i++){
                Character temp = s.charAt(i);

                int value = 0; 
                if(map.containsKey(temp)){
                    value = map.get(temp); //字元的值
                    if(i != 0 &&  (map.get(s.charAt(i-1)) < value) ){
                        value = value - (map.get(s.charAt(i-1))*2); //減去前面數本身*2
                    }
                    result = result + value;
                }
            }
        }
        return result;
    }
}
2. 對應字串,但之後記得要轉換

class Solution {
    public int romanToInt(String s) {
        int result = 0;
        if(s != ""){
            HashMap<String,Integer> map = new HashMap<String,Integer>();
            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);

            for(int i = 0 ; i<s.length() ; i++){
                String temp = s.charAt(i)+""; //把字元轉成字串
//                String temp = Character.toString(s.charAt(i)); //把字元轉成字串

                int value = 0;
                if(map.containsKey(temp)){
                    value = map.get(temp);
                    if(i != 0 &&  (map.get(s.charAt(i-1)+"") < value) ){
                        value = value - (map.get(s.charAt(i-1)+""))*2;
                    }
                    result = result + value;
                }
            }
        }
        return result;
    }
}

<補充>字元轉字串

1. String temp = s.charAt(i)+""; //把字元轉成字串
2. String temp = Character.toString(s.charAt(i)); //把字元轉成字串

3. Integer.valueOf() 方法用於將字串轉換為整數,而不是將單個"字元"轉換為整數。

<補充>HashMap containsKey() 方法


import java.util.HashMap;

class Main {
    public static void main(String[] args) {
        HashMap<Integer, String> sites = new HashMap<>();

        sites.put(1, "Google");
        sites.put(2, "Yahoo");
        sites.put(3, "Apple");
        System.out.println("sites HashMap: " + sites);

        //檢查是否存在
        if(sites.containsKey(1)) {
            System.out.printf("key %d 存在於 sites 中%n", 1); //若要使用格式化字符
            System.out.println("key " + 1 + " 存在於 sites 中");
        }

        //若不存在,則新增
        if (!sites.containsKey(4)){
            sites.put(4, "Tesla");
        }
        System.out.println("New sites HashMap:" + sites);
    }
}

<補充>格式化字符


//檢查是否存在
        if(sites.containsKey(1)) {
            System.out.printf("key %d 存在於 sites 中%n", 1); //若要使用格式化字符
            System.out.println("key " + 1 + " 存在於 sites 中");
        }