|
| 1 | +/* |
| 2 | +Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. |
| 3 | +
|
| 4 | +Symbol Value |
| 5 | +I 1 |
| 6 | +V 5 |
| 7 | +X 10 |
| 8 | +L 50 |
| 9 | +C 100 |
| 10 | +D 500 |
| 11 | +M 1000 |
| 12 | +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. |
| 13 | +
|
| 14 | +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: |
| 15 | +
|
| 16 | +I can be placed before V (5) and X (10) to make 4 and 9. |
| 17 | +X can be placed before L (50) and C (100) to make 40 and 90. |
| 18 | +C can be placed before D (500) and M (1000) to make 400 and 900. |
| 19 | +Given a roman numeral, convert it to an integer. |
| 20 | +
|
| 21 | + |
| 22 | +
|
| 23 | +Example 1: |
| 24 | +
|
| 25 | +Input: s = "III" |
| 26 | +Output: 3 |
| 27 | +Example 2: |
| 28 | +
|
| 29 | +Input: s = "IV" |
| 30 | +Output: 4 |
| 31 | +Example 3: |
| 32 | +
|
| 33 | +Input: s = "IX" |
| 34 | +Output: 9 |
| 35 | +Example 4: |
| 36 | +
|
| 37 | +Input: s = "LVIII" |
| 38 | +Output: 58 |
| 39 | +Explanation: L = 50, V= 5, III = 3. |
| 40 | +Example 5: |
| 41 | +
|
| 42 | +Input: s = "MCMXCIV" |
| 43 | +Output: 1994 |
| 44 | +Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. |
| 45 | + |
| 46 | +
|
| 47 | +Constraints: |
| 48 | +
|
| 49 | +1 <= s.length <= 15 |
| 50 | +s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M'). |
| 51 | +It is guaranteed that s is a valid roman numeral in the range [1, 3999]. |
| 52 | +*/ |
| 53 | + |
| 54 | + |
| 55 | +// functional code |
| 56 | + |
1 | 57 | class Solution {
|
2 | 58 | public int romanToInt(String s) {
|
3 |
| - int ans = 0; |
4 |
| - int n = s.length(); |
5 |
| - |
6 |
| - HashMap<Character, Integer> map = new HashMap<>(); |
7 |
| - map.put('I',1); |
8 |
| - map.put('V',5); |
9 |
| - map.put('X',10); |
10 |
| - map.put('L',50); |
11 |
| - map.put('C',100); |
12 |
| - map.put('D',500); |
13 |
| - map.put('M',1000); |
14 |
| - |
15 |
| - for(int i=0; i<n; i++){ |
16 |
| - if((i!=n-1) && (map.get(s.charAt(i)) < map.get(s.charAt(i+1)))){ |
17 |
| - ans -= map.get(s.charAt(i)); |
18 |
| - } |
19 |
| - else{ |
20 |
| - ans += map.get(s.charAt(i)); |
| 59 | + |
| 60 | + //Create HashMap to insert all the necessary roman numeral and its number |
| 61 | + HashMap<Character, Integer> ht = new HashMap<Character, Integer>(); |
| 62 | + |
| 63 | + //Now insert it |
| 64 | + ht.put('M', 1000); |
| 65 | + ht.put('D', 500); |
| 66 | + ht.put('C', 100); |
| 67 | + ht.put('L', 50); |
| 68 | + ht.put('X', 10); |
| 69 | + ht.put('V', 5); |
| 70 | + ht.put('I', 1); |
| 71 | + |
| 72 | + //This is to store all the total number of roman numeral integers |
| 73 | + int total = 0; |
| 74 | + |
| 75 | + for(int i = 0; i < s.length() - 1; i++) { |
| 76 | + //In here, if the roman numeral is let's say IV, then we first subtract 1 and add in 5. It's same logic as 5-1 = 4. |
| 77 | + if(ht.get(s.charAt(i)) < ht.get(s.charAt(i + 1))) { |
| 78 | + total -= ht.get(s.charAt(i)); |
| 79 | + } else { |
| 80 | + //If not, then simply add the number to total |
| 81 | + total += ht.get(s.charAt(i)); |
21 | 82 | }
|
22 | 83 | }
|
23 |
| - |
24 |
| - return ans; |
| 84 | + // Due to out of bound in the last character of the string, we need to add it separately |
| 85 | + return total += ht.get(s.charAt(s.length() - 1)); |
| 86 | + |
25 | 87 | }
|
26 | 88 | }
|
0 commit comments