Untitled
unknown
plain_text
2 years ago
959 B
6
Indexable
import java.io.*; import java.util.Scanner; import java.util.HashMap; public class hashConvert { int romanToInt(String s) { // Create a empty hash map. HashMap<Character, Integer> map = new HashMap<>(); // Putting value in hash map. 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); // Creating integer variable to store result. int result = 0; // initialize loop to iterate in string. for (int i = 0; i < s.length(); i++) { // Checking that current element // is not smaller then previous if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i - 1))) { result += map.get(s.charAt(i)) - 2 * map.get(s.charAt(i - 1)); } else { result += map.get(s.charAt(i)); } } // Returning the integer value of Roman number. return result; }
Editor is loading...