diff --git a/interview_prep/algorithm/java/ide_handicapped/roman_to_integer/Solution.java b/interview_prep/algorithm/java/ide_handicapped/roman_to_integer/Solution.java new file mode 100644 index 0000000..0bdc579 --- /dev/null +++ b/interview_prep/algorithm/java/ide_handicapped/roman_to_integer/Solution.java @@ -0,0 +1,36 @@ +class Solution { + public int romanToInt(String s) { + int num = 0; + int sum = 0; + for (int i = 0; i 0; i--) { + if ( (s.charAt(i) == 'V') & (s.charAt(i-1) == 'I') ) sum = sum-2; + if ( (s.charAt(i) == 'X') & (s.charAt(i-1) == 'I') ) sum = sum-2; + if ( (s.charAt(i) == 'L' ) & (s.charAt(i-1) == 'X') ) sum = sum-20; + if ( (s.charAt(i) == 'C' ) & (s.charAt(i-1) == 'X') ) sum = sum-20; + if ( (s.charAt(i) == 'M' ) & (s.charAt(i-1) == 'C') ) sum = sum-200; + if ( (s.charAt(i) == 'D') & (s.charAt(i-1) == 'C') ) sum = sum-200; + } + return sum; + } + + public static void main(String[] args) { + //https://leetcode.com/problems/roman-to-integer/ + Solution sol = new Solution(); + assert sol.romanToInt("III") == 3; + assert sol.romanToInt("LVIII") == 58; + assert sol.romanToInt("MCMXCIV") == 1994; + } +} \ No newline at end of file