I did traverse Left to right. Its Accepted .
class Solution { public int romanToInt(String s) { Map map=new HashMap<>(); map.put('I',1); map.put('V',5); map.put('X',10); map.put('L',50); map.put('C',100); map.put('D',500); map.put('M',1000); char [] romanChar=s.toCharArray(); int sum=0; int i=0; for(char ch:romanChar){ int flag=1; if(!(i==s.length()-1) ){ if (map.get(ch) < map.get(s.charAt(i + 1))) { sum = sum - map.get(ch) ; flag=0; } } if(flag==1) { sum = sum + map.get(ch); } i++; } return sum; } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I did traverse Left to right. Its Accepted .
class Solution {
public int romanToInt(String s) {
Map map=new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
char [] romanChar=s.toCharArray();
int sum=0;
int i=0;
for(char ch:romanChar){
int flag=1;
if(!(i==s.length()-1) ){
if (map.get(ch) < map.get(s.charAt(i + 1))) {
sum = sum - map.get(ch) ;
flag=0;
}
}
if(flag==1) {
sum = sum + map.get(ch);
}
i++;
}
return sum;
}
}