-
Notifications
You must be signed in to change notification settings - Fork 0
/
13_RomanNumbers.java
65 lines (65 loc) · 1.94 KB
/
13_RomanNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
===============================================
:::: DETAILS ::::
Author : Harsh Mehta, Date : June 2024, Type : Easy Problem, No. 13
===============================================
:::: PROBLEM ::::
* Given a roman numeral, convert it to an integer.
*
* Example 1:
* Input: s = "III"
* Output: 3
* Explanation: III = 3.
*
* Example 2:
* Input: s = "LVIII"
* Output: 58
* Explanation: L = 50, V= 5, III = 3.
*
* Example 3:
* Input: s = "MCMXCIV"
* Output: 1994
* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
===============================================
:::: STATS ::::
TIME : 2ms , Beats 100.00% of users with Java
MEMORY : 44.45mb Beats 73.53% of users with Java
===============================================
:::: SOLUTION ::::
* This code converts a Roman numeral string to an integer.
* It first maps each Roman numeral to its corresponding integer value.
* Then it sums these values while accounting for subtractive combinations (e.g., IV = 4) by checking if a numeral is less than the next one.
===============================================
*/
class Solution {
public int romanToInt(String s) {
int val = 0, p = 0;
for (char ch: s.toCharArray()) {
switch(ch)
{
case 'I':
val+=1;p=1;
break;
case 'V':
val+=(p==1)?3:5;p=5;
break;
case 'X':
val+=(p==1)?8:10;p=10;
break;
case 'L':
val+=(p==10)?30:50;p=50;
break;
case 'C':
val+=(p==10)?80:100;p=100;
break;
case 'D':
val+=(p==100)?300:500;p=500;
break;
case 'M':
val+=(p==100)?800:1000;p=1000;
break;
}
}
return val;
}
}