iT邦幫忙

2021 iThome 鐵人賽

DAY 11
0
自我挑戰組

30天 Leetcode解題之路系列 第 11

Day 11 - Roman to Integer

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
廢話不多說開始今天的解題Day~


13. Roman to Integer

Question

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

For example, 2 is written as II in Roman numeral, just two one's 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

Example1

Input: s = "III"
Output: 3

Example2

Input: s = "IV"
Output: 4

Example3

Input: s = "IX"
Output: 9

Example4

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

Example5

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

Constraints

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

解題

題目

首先先簡單的翻譯一下題目
給你羅馬數字要轉回成十進位的數字表示,然後I可以放在VX的前面,X可以放在LC的前面,而C可以放在DM的前面。
像是:IV,V代表的是5,I代表的是1,IV則代表的是4 (5-1),以此類推。

Think

作法大致上是這樣

  • 先把str的羅馬字串拆開成一位一位,然後從陣列的後面讀回來,因為要判斷需要加法還是減法。
  • 除了讀進來的第一個羅馬數字以外,每個羅馬數字都要判斷他所代表的數字有沒有小於前一個讀進來的羅馬數字,有的話就扣掉,沒有的話就加上去,最後回傳sum

Code

Python

class Solution:
    def romanToInt(self, s: str) -> int:
        Symbol_dict = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
        
        sum = 0
        list_s = list(s)
        
        for index in range(len(list_s)-1, -1, -1):
            if index == (len(list_s)-1):
                sum += Symbol_dict[list_s[index]]
            else:
                if Symbol_dict[list_s[index+1]] > Symbol_dict[list_s[index]]:
                    sum -= Symbol_dict[list_s[index]]
                else:
                    sum += Symbol_dict[list_s[index]]

        return sum

C

int romanToInt(char * s){
    int Symbol_dict[] = {1, 5, 10, 50, 100, 500, 1000};
    int sum = 0;
    int Symbol_dict_index1 = -1, Symbol_dict_index2 = -1;
    
    for(int index=strlen(s)-1; index>=0 ; index--) {
        printf("%c\n", s[index]);
        
        if (s[index] == 'I'){
            Symbol_dict_index1 = 0;
        } else if (s[index] == 'V'){
            Symbol_dict_index1 = 1;
        } else if (s[index] == 'X'){
            Symbol_dict_index1 = 2;
        } else if (s[index] == 'L'){
            Symbol_dict_index1 = 3;
        } else if (s[index] == 'C'){
            Symbol_dict_index1 = 4;
        } else if (s[index] == 'D'){
            Symbol_dict_index1 = 5;
        } else if (s[index] == 'M'){
            Symbol_dict_index1 = 6;
        }


        if (index == (strlen(s)-1)){
            sum += Symbol_dict[Symbol_dict_index1];
        } else {
            if (s[index+1] == 'I'){
                Symbol_dict_index2 = 0;
            } else if (s[index+1] == 'V'){
                Symbol_dict_index2 = 1;
            } else if (s[index+1] == 'X'){
                Symbol_dict_index2 = 2;
            } else if (s[index+1] == 'L'){
                Symbol_dict_index2 = 3;
            } else if (s[index+1] == 'C'){
                Symbol_dict_index2 = 4;
            } else if (s[index+1] == 'D'){
                Symbol_dict_index2 = 5;
            } else if (s[index+1] == 'M'){
                Symbol_dict_index2 = 6;
            }
            
            if (Symbol_dict[Symbol_dict_index2] > Symbol_dict[Symbol_dict_index1]){
                sum -= Symbol_dict[Symbol_dict_index1];
            } else {
                sum += Symbol_dict[Symbol_dict_index1];
            }
        }
   
    }
    return sum;

}

Result

  • Python

  • C

大家明天見/images/emoticon/emoticon29.gif


上一篇
Day 10 - Valid Sudoku
下一篇
Day 12 - Length of Last Word
系列文
30天 Leetcode解題之路30

尚未有邦友留言

立即登入留言