0% found this document useful (0 votes)
10 views1 page

Roman To Int

The document contains a Java class that defines a method to convert a Roman numeral string to an integer value. The method uses a map to store the numeric values of each Roman numeral character and iterates through the string, adding or subtracting values based on the order and relative magnitude of adjacent characters.

Uploaded by

sukanya moorthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Roman To Int

The document contains a Java class that defines a method to convert a Roman numeral string to an integer value. The method uses a map to store the numeric values of each Roman numeral character and iterates through the string, adding or subtracting values based on the order and relative magnitude of adjacent characters.

Uploaded by

sukanya moorthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Solution {

public int romanToInt(String s) {


Map<Character, Integer> 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);

int ans=0; // IXXI


for(int i=0; i<s.length();i++)
{
if(i < s.length()-1 && map.get(s.charAt(i)) < map.get(s.charAt(i+1)))
{
ans-=map.get(s.charAt(i));
}
else
{
ans+=map.get(s.charAt(i));
}

return ans;

}
}

You might also like