Repository navigation
Expand file tree
/
Copy pathsolution.java
More file actions
83 lines (82 loc) · 2.23 KB
/
Copy pathsolution.java
File metadata and controls
83 lines (82 loc) · 2.23 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 13. Roman to Integer
// https://leetcode.com/problems/roman-to-integer/
// Easy | Java | Accepted 2026-09-02
// Runtime 3 ms | Memory 47 MB
class Solution {
public int romanToInt(String s) {
int ind = 0;
int count = 0;
while(ind<s.length())
{
if(s.charAt(ind)=='V')
{
count+=5;
}
if(s.charAt(ind)=='L')
{
count+=50;
}
if(s.charAt(ind)=='D')
{
count+=500;
}
if(s.charAt(ind)=='M')
{
count+=1000;
}
if(s.charAt(ind)=='I')
{
if(ind+1<s.length() && s.charAt(ind+1)=='V')
{
count+=4;
ind++;
}
else if(ind+1<s.length() && s.charAt(ind+1)=='X')
{
count+=9;
ind++;
}
else
{
count++;
}
}
else if(s.charAt(ind)=='X')
{
if(ind+1<s.length() && s.charAt(ind+1)=='L')
{
count+=40;
ind++;
}
else if(ind+1<s.length() && s.charAt(ind+1)=='C')
{
count+=90;
ind++;
}
else
{
count+=10;
}
}
else if(s.charAt(ind)=='C')
{
if(ind+1<s.length() && s.charAt(ind+1)=='D')
{
count+=400;
ind++;
}
else if(ind+1<s.length() && s.charAt(ind+1)=='M')
{
count+=900;
ind++;
}
else
{
count+=100;
}
}
ind++;
}
return count;
}
}