Skip to content

Commit 65f905f

Browse files
committed
Add new problem - Roman to Integer.
1 parent 09d59c3 commit 65f905f

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Leetcode
22

3-
### 📖 [Top 100 Interview Questions](https://leetcode.com/problemset/top-interview-questions/) (9/100 Solved) ✅
3+
### 📖 [Top 100 Interview Questions](https://leetcode.com/problemset/top-interview-questions/) (10/100 Solved) ✅
44

55
1. [Two Sum](<Top 100 Interview Questions/Two Sum>) (Easy)
66
2. [Add Two Numbers](<Top 100 Interview Questions/Add Two Numbers>) (Medium)
@@ -11,3 +11,4 @@
1111
7. [String to Integer (atoi)](<Top 100 Interview Questions/String to Integer (atoi)>) (Medium)
1212
8. [Regular Expression Matching](<Top 100 Interview Questions/Regular Expression Matching>) (Hard)
1313
9. [Container With Most Water](<Top 100 Interview Questions/Container With Most Water>) (Medium)
14+
10. [Roman to Integer](<Top 100 Interview Questions/Roman to Integer>) (Easy)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) (Easy)
2+
3+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
4+
5+
```
6+
Symbol Value
7+
I 1
8+
V 5
9+
X 10
10+
L 50
11+
C 100
12+
D 500
13+
M 1000
14+
```
15+
16+
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`.
17+
18+
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:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given a roman numeral, convert it to an integer.
25+
26+
### Example 1:
27+
28+
```
29+
Input: s = "III"
30+
Output: 3
31+
```
32+
33+
### Example 2:
34+
35+
```
36+
Input: s = "IV"
37+
Output: 4
38+
```
39+
40+
### Example 3:
41+
42+
```
43+
Input: s = "IX"
44+
Output: 9
45+
```
46+
47+
### Example 4:
48+
49+
```
50+
Input: s = "LVIII"
51+
Output: 58
52+
Explanation: L = 50, V= 5, III = 3.
53+
```
54+
55+
### Example 5:
56+
57+
```
58+
Input: s = "MCMXCIV"
59+
Output: 1994
60+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
61+
```
62+
63+
### Constraints:
64+
65+
* `1 <= s.length <= 15`
66+
* `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
67+
* It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
final class Solution {
2+
func romanToInt(_ string: String) -> Int {
3+
let map: [Character: Int] = [
4+
"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000
5+
]
6+
7+
var number = 0
8+
var index = string.startIndex
9+
while index != string.endIndex {
10+
guard let char = map[string[index]] else { return -1 }
11+
12+
let next = string.index(after: index)
13+
if next < string.endIndex, let nextChar = map[string[next]], char < nextChar {
14+
number += nextChar - char
15+
index = string.index(after: next)
16+
} else {
17+
number += char
18+
index = next
19+
}
20+
}
21+
return number
22+
}
23+
}

0 commit comments

Comments
 (0)