> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/09-16-2021-no.-13.-roman-to-integer.md).

# 09/16/2021 No. 13. Roman to Integer

For this problem, you have to be very careful when typing!!!!!

switch&#x20;

HashMap

String length

Solution 1:

class Solution { public int romanToInt(String s) {

```
    if (s == null || s.length() == 0) {//corner case
        return 0;
    }

    char c  = s.charAt(0); //charAt method return char at index
    int sum = getValue(c);

    for (int i = 1; i < s.length(); i++){//string.length()  different from the array.length;
        sum += getValue(s.charAt(i));

        if (getValue(s.charAt(i)) > getValue(s.charAt(i-1))){
            sum = sum - 2 * getValue(s.charAt(i-1));
        }
    }

    return sum;

}



private int getValue(char c){
    switch(c){
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
    }
    return 0;
}
```

}

Better solution: iterate from end to the beginning:

class Solution { public int romanToInt(String s) {

```
    if (s == null || s.length() == 0){
        return 0;
    }

    int len = s.length();
    int sum = get(s.charAt(len - 1));

    for (int i = len - 2; i >= 0; i--){
        if (get(s.charAt(i)) < get(s.charAt(i + 1))){
            sum = sum - get(s.charAt(i));
        } else {
            sum = sum + get(s.charAt(i));
        }
    }
    return sum;
}

private int get(char c){
    switch(c){
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
    }
    return 0;
}
```

}

class Solution {&#x20;

public int romanToInt(String s) {&#x20;

//corner case&#x20;

if(s == null || s.length() == 0) return 0;&#x20;

int len = s.length() -1;&#x20;

int sum = getValue(s.charAt(len));

```
    for (int i = len -1; i >= 0; i--){
        if(getValue(s.charAt(i)) < getValue(s.charAt(i+1))){
            sum -= getValue(s.charAt(i));
        }else{
            sum += getValue(s.charAt(i));
        }
    }     
    return sum;                 
```

}

private int getValue(char c){

```
 switch (c){
     case 'I': return 1;
     case 'V': return 5;
     case 'X': return 10;
     case 'L': return 50;
     case 'C': return 100;
     case 'D': return 500;
     case 'M': return 1000;
 }
 return 0;// don't forget this.
```

} }

Last solution: using hashMap;

class Solution { public int romanToInt(String s) {

```
    HashMap<Character, Integer> map = new HashMap<>();
    map.put('I', 1);
    map.put('V', 5);
    map.put('X', 10);
    map.put('L', 50);
    map.put('C', 100);
    map.put('D', 500);
    map.put('M', 1000);


    int len  = s.length();
    int sum = map.get(s.charAt(len - 1));

    for (int i = len - 2; i >= 0; i--){
        if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))){
            sum = sum - map.get(s.charAt(i));
        } else {
            sum = sum + map.get(s.charAt(i));
        }

    }

     return sum;
```

} }

* Time complexity : O(1).

  As there is a finite set of roman numerals, the maximum number possible number can be `3999`, which in roman numerals is `MMMCMXCIX`. As such the time complexity is O(1)O(1).

  If roman numerals had an arbitrary number of symbols, then the time complexity would be proportional to the length of the input, i.e. O(n)O(n). This is assuming that looking up the value of each symbol is O(1).
* Space complexity : O(1).

  Because only a constant number of single-value variables are used, the space complexity is O(1)O(1).
