LeetCode 13. Roman to Integer ( C )-Easy

此題為前一題 LeetCode 12. Integer to Roman 的延伸,這次為給定一個羅馬數字轉換成阿拉伯數字。

原文題目如下

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 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 1:

Input: s = "III"
Output: 3
Example 2:

Input: s = "IV"
Output: 4
Example 3:

Input: s = "IX"
Output: 9
Example 4:

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

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].

解題策略為依序拆解,先從前面的 ‘M’ 轉換成千,再轉換 ‘CM’ , ‘CD’ 900 和 400,再轉換 ‘D’以此類推。

下面是我的程式碼

int romanToInt(char * s){
    int num = 0;
    for(int i = 0;i<strlen(s);i++){
        if( s[i] == 'M' ){
            num+=1000;
        }
        
        else if( s[i] == 'C' ){
            if(i+1<strlen(s)){
                if( s[i+1] == 'M' ){
                    num+=900;
                    i++;
                }
                else if( s[i+1] == 'D' ){
                    num+=400;
                    i++;
                }
                else
                    num+=100;
            }
            else{
                num+=100;
            }
        }
        else if( s[i] == 'D' )
            num+=500;
        
        else if( s[i] == 'X' ){
            if(i+1<strlen(s)){
                if( s[i+1] == 'C' ){
                    num+=90;
                    i++;
                }
                else if( s[i+1] == 'L' ){
                    num+=40;
                    i++;
                }
                else
                    num+=10;
            }
            else{
                num+=10;
            }
        }
        else if( s[i] == 'L' )
            num+=50;
        
        else if( s[i] == 'I' ){
            if(i+1<strlen(s)){
                if( s[i+1] == 'X' ){
                    num+=9;
                    i++;
                }
                else if( s[i+1] == 'V' ){
                    num+=4;
                    i++;
                }
                else
                    num+=1;
            }
            else{
                num+=1;
            }
        }
        else if( s[i] == 'V' )
            num+=5;
    }
    return num;
}

下面是時間與空間之消耗

Runtime: 4 ms, faster than 84.35 % of C online submissions for Roman to Integer.

Memory Usage: 5.8 MB, less than 58.45 % of C online submissions for Roman to Integer.

下一題連結 : LeetCode 14. Longest Common Prefix ( C )-Easy

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *