We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents c0b75a3 + 5ac3e02 commit 17d6518Copy full SHA for 17d6518
13. Roman to Integer
@@ -0,0 +1,34 @@
1
+class Solution {
2
+ public int romanToInt(String s) {
3
+
4
+ HashMap<Character,Integer> map=new HashMap<>();
5
+ map.put('I',1);
6
+ map.put('V',5);
7
+ map.put('X',10);
8
+ map.put('L',50);
9
+ map.put('C',100);
10
+ map.put('D',500);
11
+ map.put('M',1000);
12
13
+ int sum=0;
14
+ for(int i=0;i<s.length();i++){
15
+ char ch=s.charAt(i);
16
+ int a=map.get(ch);
17
18
+ if(i+1<s.length()){
19
+ int b=map.get(s.charAt(i+1));
20
21
+ if(a>=b){
22
+ sum=sum+a;
23
+ }
24
+ else{
25
+ sum=sum-a;
26
27
28
29
30
31
32
+ return sum;
33
34
+}
0 commit comments