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


<<:  EP 11 - [TDD] 建立 Gateway

>>:  DAY 14- 《公钥密码》-RSA(2)

电子书阅读器上的浏览器 [Day14] 中文直排阅读模式

这功能对於浏览器来说,应该是个没人(或很少人)想过会存在的功能。 从十几二十年前开始有浏览器以来,浏...

[第十八天]从0开始的UnityAR手机游戏开发-介绍Animation

Unity有内建的动画系统,简单的小动画可以使用Animation完成 本次章节会先用Cube做出简...

[Day 2] SRE - 你的服务死後不要让人担心嘛

graceful shutdown 在关闭服务前,在服务内部以做完该做的事情,使得服务得以善终。 在...

[Lesson9] Firebase

首先到下列的网站 https://console.firebase.google.com/?hl=z...

Day 18 AWS云端实作起手式第八弹 让开机器变得很自动自发Auto Scaling-WriteNode设置

Auto-Scaling设置内容较多,我们快速回顾一下昨天的架构图: 针对外面的流量,我们提供使用者...