Skip to content

Commit 226f60a

Browse files
author
zhangbo54
committed
13. Roman to Integer
1 parent 94d0d67 commit 226f60a

File tree

5 files changed

+357
-0
lines changed

5 files changed

+357
-0
lines changed

src/leetcode/_12_/md.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### [12\. Integer to Roman](https://leetcode.com/problems/integer-to-roman/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
7+
8+
```
9+
Symbol Value
10+
I 1
11+
V 5
12+
X 10
13+
L 50
14+
C 100
15+
D 500
16+
M 1000```
17+
18+
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`.
19+
20+
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:
21+
22+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. 
23+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. 
24+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25+
26+
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
27+
28+
**Example 1:**
29+
30+
```
31+
Input: 3
32+
Output: "III"```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: 4
38+
Output: "IV"```
39+
40+
**Example 3:**
41+
42+
```
43+
Input: 9
44+
Output: "IX"```
45+
46+
**Example 4:**
47+
48+
```
49+
Input: 58
50+
Output: "LVIII"
51+
Explanation: L = 50, V = 5, III = 3.
52+
```
53+
54+
**Example 5:**
55+
56+
```
57+
Input: 1994
58+
Output: "MCMXCIV"
59+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.```
60+
61+
62+
#### Solution
63+
64+
Language: **Java**
65+
66+
```java
67+
class Solution {
68+
   public String intToRoman(int num) {
69+
       String M[] = {"", "M", "MM", "MMM"};
70+
       String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
71+
       String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
72+
       String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
73+
       return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
74+
  }
75+
}
76+
```

src/leetcode/_13_/Main.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode._13_;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* Created by zhangbo54 on 2019-03-04.
8+
*/
9+
public class Main {
10+
public static void main(String[] args) {
11+
Solution solution = new Solution();
12+
System.out.println(solution.romanToInt("LVIII"));
13+
// Integer.parseInt("1" + Integer.MAX_VALUE);
14+
}
15+
16+
static
17+
class Solution {
18+
public int romanToInt(String s) {
19+
int result = 0;
20+
List<String> M = Arrays.asList("MMM", "MM", "M");
21+
List<String> C = Arrays.asList("CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C");
22+
List<String> X = Arrays.asList("XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X");
23+
List<String> I = Arrays.asList("IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I");
24+
List<List<String>> list = Arrays.asList(M, C, X, I);
25+
int size = 1000;
26+
for (List<String> item : list) {
27+
for (int i = 0; i < item.size(); i++) {
28+
if (s.startsWith(item.get(i))) {
29+
result += (item.size() - i) * size;
30+
s = s.substring(item.get(i).length());
31+
}
32+
}
33+
size /= 10;
34+
}
35+
return result;
36+
}
37+
}
38+
}

src/leetcode/_13_/Main2.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode._13_;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* Created by zhangbo54 on 2019-03-04.
8+
*/
9+
public class Main2 {
10+
public static void main(String[] args) {
11+
Solution solution = new Solution();
12+
System.out.println(solution.romanToInt("LVIII"));
13+
// Integer.parseInt("1" + Integer.MAX_VALUE);
14+
}
15+
16+
static
17+
class Solution {
18+
public int romanToInt(String s) {
19+
int result = 0;
20+
for (int i = 0; i < s.length(); i++) {
21+
int cur = getInt(s.charAt(i));
22+
if (i + 1 < s.length() && getInt(s.charAt(i + 1)) > getInt(s.charAt(i))) {
23+
result -= cur;
24+
} else {
25+
result += cur;
26+
}
27+
}
28+
return result;
29+
}
30+
31+
private int getInt(char ch) {
32+
switch (ch) {
33+
case 'I':
34+
return 1;
35+
case 'V':
36+
return 5;
37+
case 'X':
38+
return 10;
39+
case 'L':
40+
return 50;
41+
case 'C':
42+
return 100;
43+
case 'D':
44+
return 500;
45+
case 'M':
46+
return 1000;
47+
default:
48+
return 0;
49+
}
50+
}
51+
}
52+
}

src/leetcode/_13_/solution1.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### [13\. Roman to Integer](https://leetcode.com/problems/roman-to-integer/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
7+
8+
```
9+
Symbol Value
10+
I 1
11+
V 5
12+
X 10
13+
L 50
14+
C 100
15+
D 500
16+
M 1000```
17+
18+
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`.
19+
20+
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:
21+
22+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. 
23+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. 
24+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25+
26+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
27+
28+
**Example 1:**
29+
30+
```
31+
Input: "III"
32+
Output: 3```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: "IV"
38+
Output: 4```
39+
40+
**Example 3:**
41+
42+
```
43+
Input: "IX"
44+
Output: 9```
45+
46+
**Example 4:**
47+
48+
```
49+
Input: "LVIII"
50+
Output: 58
51+
Explanation: L = 50, V= 5, III = 3.
52+
```
53+
54+
**Example 5:**
55+
56+
```
57+
Input: "MCMXCIV"
58+
Output: 1994
59+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.```
60+
61+
62+
#### Solution
63+
64+
Language: **Java**
65+
66+
```java
67+
class Solution {
68+
   public int romanToInt(String s) {
69+
       int result = 0;
70+
       List<String> M = Arrays.asList("MMM", "MM", "M");
71+
       List<String> C = Arrays.asList("CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C");
72+
       List<String> X = Arrays.asList("XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X");
73+
       List<String> I = Arrays.asList("IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I");
74+
       List<List<String>> list = Arrays.asList(M, C, X, I);
75+
       int size = 1000;
76+
       for (List<String> item : list) {
77+
           for (int i = 0; i < item.size(); i++) {
78+
               if (s.startsWith(item.get(i))) {
79+
                   result += (item.size() - i) * size;
80+
                   s = s.substring(item.get(i).length());
81+
              }
82+
          }
83+
           size /= 10;
84+
      }
85+
       return result;
86+
  }
87+
}
88+
           case 'I':
89+
```

src/leetcode/_13_/solution2.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### [13\. Roman to Integer](https://leetcode.com/problems/roman-to-integer/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
7+
8+
```
9+
Symbol Value
10+
I 1
11+
V 5
12+
X 10
13+
L 50
14+
C 100
15+
D 500
16+
M 1000```
17+
18+
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`.
19+
20+
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:
21+
22+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. 
23+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. 
24+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25+
26+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
27+
28+
**Example 1:**
29+
30+
```
31+
Input: "III"
32+
Output: 3```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: "IV"
38+
Output: 4```
39+
40+
**Example 3:**
41+
42+
```
43+
Input: "IX"
44+
Output: 9```
45+
46+
**Example 4:**
47+
48+
```
49+
Input: "LVIII"
50+
Output: 58
51+
Explanation: L = 50, V= 5, III = 3.
52+
```
53+
54+
**Example 5:**
55+
56+
```
57+
Input: "MCMXCIV"
58+
Output: 1994
59+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.```
60+
61+
62+
#### Solution
63+
64+
Language: **Java**
65+
66+
```java
67+
class Solution {
68+
   public int romanToInt(String s) {
69+
       int result = 0;
70+
       for (int i = 0; i < s.length(); i++) {
71+
           int cur = getInt(s.charAt(i));
72+
           if (i + 1 < s.length() && getInt(s.charAt(i + 1)) > getInt(s.charAt(i))) {
73+
               result -= cur;
74+
          } else {
75+
               result += cur;
76+
          }
77+
      }
78+
       return result;
79+
  }
80+
81+
   private int getInt(char ch) {
82+
       switch (ch) {
83+
           case 'I':
84+
               return 1;
85+
           case 'V':
86+
               return 5;
87+
           case 'X':
88+
               return 10;
89+
           case 'L':
90+
               return 50;
91+
           case 'C':
92+
               return 100;
93+
           case 'D':
94+
               return 500;
95+
           case 'M':
96+
               return 1000;
97+
           default:
98+
               return 0;
99+
      }
100+
  }
101+
}
102+
```

0 commit comments

Comments
 (0)