LeetCode Js-13. Roman to Integer

    LeetCode Js-String

    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.

    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.

    Example :

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

    Code 1:

    var romanToInt = function(s) {
        const romanMap: Record =  { //建立羅馬轉換表
        'I':1,
        'V':5,
        'X':10,
        'L':50,
        'C':100,
        'D':500,
        'M':1000
        }
      
        let total: number = 0 //宣告待數字加總的空間
      
        for (let i = 0; i < s.length; i++) { //input字串決定i迴圈次數
          if (romanMap[s[i]] < romanMap[s[i+1]]) { //如果 i 小於 i+1
            total += romanMap[s[i+1]] - romanMap[s[i]] //羅馬數字的規則,後面數字比前面大,須將後面減前面值
            i++ //i進到下一個順位
          } else {
            total += romanMap[s[i]]
          }
        }    
      return total              
    }
    ;

    FlowChart:

    i = for loop
    s.length = 7 ("MCMXCIV")
    
    step.1
    i = 0
    romanMap[s[i]] = "M" = 1000
    romanMap[s[i+1]] = "C" = 100
    else {total = 0 + 1000 = 1000}
    
    step.2
    i = 1
    romanMap[s[i]] = "C" = 100
    romanMap[s[i+1]] = "M" = 1000
    if (100 < 1000) {total = 1000 + 1000 - 100 = 1900}
    i++
    
    step.3
    i = 3
    romanMap[s[i]] = "X" = 10
    romanMap[s[i+1]] = "C" = 100
    if (10 < 100) {total = 1900 + 100 - 10 = 1990}
    i++
    
    step.4
    i = 5
    romanMap[s[i]] = "I" = 1
    romanMap[s[i+1]] = "V" = 5
    if (1 < 5) {total = 1990 + 5 - 1 = 1994}
    i++
    
    step.5
    i = 7 
    for i(7) < s.length(7) //條件不成立
    break
    

    Answer:

    Output = 1994
    

    Code 2:
    var romanToInt = function(s) {
    const romanMap: Record = { //建立羅馬轉換表
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
    }
    let i: number = 0,
    total: number = 0 //宣告待數字加總的空間

    while (i < s.length) { if (romanMap[s[i]] < romanMap[s[i + 1]] && i != (s.length - 1)) { total += romanMap[s[i + 1]] - romanMap[s[i]]; i += 2; } else { total += romanMap[s[i]]; i++; } } return total };