Skip to content

Commit 728cd1c

Browse files
committed
solved: 13. Roman to Integer
Signed-off-by: rajput-hemant <[email protected]>
1 parent fa4b81f commit 728cd1c

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

TOPICWISE.md

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
| # | Solution | Tags | Difficulty | Remark |
4646
| :------: | :-----------------------------------------------------------: | :-------------------------------------------: | :---------: | :----: |
47+
| **0013** | [Roman to Integer][13] | String, Math, Hash Table | ![][easy] | |
4748
| **0058** | [Length of Last Word][58] | String | ![][easy] | |
4849
| **0072** | [Edit Distance][72] | String, DP | ![][hard] | |
4950
| **0344** | [Reverse String][344] | Two Pointers, String, Recursion | ![][easy] | |
@@ -61,6 +62,7 @@
6162
| # | Solution | Tags | Difficulty | Remark |
6263
| :------: | :---------------------------------: | :-------------------------------------------: | :---------: | :----: |
6364
| **0001** | [Two Sum][1] | Array, Hash Table | ![][easy] | |
65+
| **0013** | [Roman to Integer][13] | String, Math, Hash Table | ![][easy] | |
6466
| **0389** | [Find the Difference][389] | Hash Table, String, Bit Manipulation, Sorting | ![][easy] | |
6567
| **0442** | [Find All Duplicates][442] | Array, Hash Table | ![][medium] | |
6668
| **0653** | [Two Sum IV - Input is a BST ][653] | Tree, DFS, BST, Binary Tree | ![][easy] | |
@@ -92,6 +94,7 @@
9294
| **0002** | [Add Two Numbers][2] | Linked List, Math, Recursion | ![][medium] | |
9395
| **0007** | [Reverse Integer][7] | Math | ![][medium] | |
9496
| **0009** | [Palindrome Number][9] | Math | ![][easy] | |
97+
| **0013** | [Roman to Integer][13] | String, Math, Hash Table | ![][easy] | |
9598
| **0070** | [Climbing Stairs][70] | Math, DP, Memorization | ![][easy] | |
9699
| **0172** | [Factorial Trailiing Zeroes][172] | Math | ![][medium] | |
97100
| **0231** | [Power of Two][231] | Math, Bit Manipulation, Recursion | ![][easy] | |
@@ -641,6 +644,7 @@
641644
[4]: ./src/0001-0100/004%20-%20Median%20of%20Two%20Sorted%20Arrays/
642645
[7]: ./src//0001-0100/007%20-%20Reverse%20Integer/
643646
[9]: ./src/0001-0100/009%20-%20Palindrome%20Number/
647+
[13]: ./src/0001-0100/013%20-%20Roman%20to%20Integer/
644648
[24]: ./src/0001-0100/024%20-%20Swap%20Nodes%20in%20Pairs/
645649
[26]: ./src/0001-0100/026%20-%20Remove%20Duplicates%20from%20Sorted%20Array/
646650
[27]: ./src/0001-0100/027%20-%20Remove%20Element/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 13. Roman to Integer [![share]](https://leetcode.com/problems/roman-to-integer/)
2+
3+
![][easy]
4+
5+
## Problem Statement:
6+
7+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
8+
9+
```
10+
Symbol Value
11+
I 1
12+
V 5
13+
X 10
14+
L 50
15+
C 100
16+
D 500
17+
M 1000
18+
```
19+
20+
For example, `2` is written as `II` in Roman numeral, just two ones added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
21+
22+
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:
23+
24+
- I can be placed before `V` (5) and `X` (10) to make 4 and 9.
25+
- X can be placed before `L` (50) and `C` (100) to make 40 and 90.
26+
- C can be placed before `D` (500) and `M` (1000) to make 400 and 900.
27+
Given a roman numeral, convert it to an integer.
28+
29+
### Example 1:
30+
31+
```
32+
Input: s = "III"
33+
Output: 3
34+
Explanation: III = 3.
35+
```
36+
37+
### Example 2:
38+
39+
```
40+
Input: s = "LVIII"
41+
Output: 58
42+
Explanation: L = 50, V= 5, III = 3.
43+
```
44+
45+
### Example 3:
46+
47+
```
48+
Input: s = "MCMXCIV"
49+
Output: 1994
50+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
51+
```
52+
53+
### Constraints:
54+
55+
```- 1 <= s.length <= 15
56+
- s contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
57+
- It is guaranteed that s is a valid roman numeral in the range [`1, 3999]`.
58+
```
59+
60+
## Solutions:
61+
62+
### [_Java_](./RomanToInteger.java)
63+
64+
```java
65+
public int romanToInt(String s) {
66+
Map<Character, Integer> map = Map.of(
67+
'I', 1,
68+
'V', 5,
69+
'X', 10,
70+
'L', 50,
71+
'C', 100,
72+
'D', 500,
73+
'M', 1000);
74+
int res = map.get(s.charAt(s.length() - 1));
75+
for (int i = s.length() - 2; i >= 0; i--) {
76+
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1)))
77+
res -= map.get(s.charAt(i));
78+
else
79+
res += map.get(s.charAt(i));
80+
}
81+
return res;
82+
}
83+
```
84+
85+
### [_..._]()
86+
87+
```
88+
89+
```
90+
91+
<!----------------------------------{ link }--------------------------------->
92+
93+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
94+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
95+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
96+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.Map;
2+
3+
public class RomanToInteger {
4+
public int romanToInt(String s) {
5+
Map<Character, Integer> map = Map.of(
6+
'I', 1,
7+
'V', 5,
8+
'X', 10,
9+
'L', 50,
10+
'C', 100,
11+
'D', 500,
12+
'M', 1000);
13+
int res = map.get(s.charAt(s.length() - 1));
14+
for (int i = s.length() - 2; i >= 0; i--) {
15+
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1)))
16+
res -= map.get(s.charAt(i));
17+
else
18+
res += map.get(s.charAt(i));
19+
}
20+
return res;
21+
}
22+
}

src/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
| **0004** | [Median of Two Sorted Arrays][4] | Array, Binary Search, Divide & Conquer | ![][hard] | |
1818
| **0007** | [Reverse Integer][7] | Math | ![][medium] | |
1919
| **0009** | [Palindrome Number][9] | Math | ![][easy] | |
20+
| **0013** | [Roman to Integer][13] | String, Math, Hash Table | ![][easy] | |
2021
| **0024** | [Swap Nodes in Pairs][24] | Linked List, Recursion | ![][medium] | |
2122
| **0026** | [Remove Duplicates][26] | Array, Two Pointers | ![][easy] | |
2223
| **0027** | [Remove Element][27] | Array, Two Pointers | ![][easy] | |
@@ -87,6 +88,7 @@
8788
[4]: ./0001-0100/004%20-%20Median%20of%20Two%20Sorted%20Arrays/
8889
[7]: ./0001-0100/007%20-%20Reverse%20Integer/
8990
[9]: ./0001-0100/009%20-%20Palindrome%20Number/
91+
[13]: ./0001-0100/013%20-%20Roman%20to%20Integer/
9092
[24]: ./0001-0100/024%20-%20Swap%20Nodes%20in%20Pairs/
9193
[26]: ./0001-0100/026%20-%20Remove%20Duplicates%20from%20Sorted%20Array/
9294
[27]: ./0001-0100/027%20-%20Remove%20Element/

0 commit comments

Comments
 (0)