LeetCode 13. Roman to Integer, 羅馬數字轉整數 ,C#

前言

本文介紹了 LeetCode 第 13 題 , “Roman to Integer”, 也就是 “羅馬數字轉整數” 的問題.

本文使用 C# 語言完成題目。

題目

English

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, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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. Input is guaranteed to be within the range from 1 to 3999.

Example 1:
Input: “III”
Output: 3

Example 2:
Input: “IV”
Output: 4

Example 3:
Input: “IX”
Output: 9

Example 4:
Input: “LVIII”
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 5:
Input: “MCMXCIV”
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

中文

LeetCode 13. 羅馬數字轉整數

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

字符          數值
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 。

通常情況下,羅馬數字中小的數字在大的數字的右邊。但也存在特例,例如 4 不寫做 IIII,而是 IV。數字 1 在數字 5 的左邊,所表示的數等於大數 5 減小數 1 得到的數值 4 。同樣地,數字 9 表示爲 IX。這個特殊的規則只適用於以下六種情況:

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

示例 1:

輸入: “III”
輸出: 3

示例 2:

輸入: “IV”
輸出: 4

示例 3:

輸入: “IX”
輸出: 9

示例 4:

輸入: “LVIII”
輸出: 58
解釋: L = 50, V= 5, III = 3.

示例 5:

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

解決方案

方法與第12題類似,只需要從頭到尾遍歷字符串,並依次轉化爲數字求和,即可得到最終的值。

參考代碼:

    public int RomanToInt(string s)
    {
        string[] romans = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        int[] nums = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
        int result = 0;
        int sp = 0; //sPointer
        int rnp = 0; //romans and nums pointer
        int n = s.Length;
        while (sp < n)
        {
            if (s.IndexOf(romans[rnp], sp) == sp)
            {
                result += nums[rnp];
                sp += romans[rnp].Length;
            }
            else rnp++;
        }
        return result;
    }

執行結果

執行結果 通過。 執行用時: 100ms, 內存消耗 26.6M

複雜度分析

時間複雜度:O(n)

遍歷長度爲n的字符串。考慮到題目範圍爲1-3999,也可以認爲是O(1).

空間複雜度:O(1)

所用空間大小爲固定值。

參考資料彙總

題目:

https://leetcode-cn.com/problems/roman-to-integer/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章