MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / romanToNumber

Method romanToNumber

Java/roman-to-integer.java:28–53  ·  view source on GitHub ↗
(String roman)

Source from the content-addressed store, hash-verified

26 }
27
28 public static int romanToNumber(String roman){//Converts a roman numerial into a number
29 int results = 0;
30 for (int i = 0; i < roman.length(); i++) {//iterate through all the characters
31 int cur = GetNum(roman.charAt(i));//Gets the integer of the roman number
32 if (cur == -1) {//checks if the cur num is -1, if it is -1, it means that it is an invalid character
33 return -1;
34 }
35 if (i + 1 < roman.length()) {//checks if the next index is out of bounds
36 int next = GetNum(roman.charAt(i + 1));//gets the next character character
37 if (next == -1) {//checks if the cur num is -1, if it is -1, it means that it is an invalid character
38 return -1;
39 }
40 if (cur >= next) {//compares the current number to the next number
41 results += cur;//if it is bigger, simply add the current number
42 }
43 else {//if the current number is smaller than the next number
44 results += (next - cur);//subtract the next number by the current number
45 i++;//increment the index, since the next number has already been used
46 }
47 }
48 else {//only here to add the last number to the results
49 results += cur;//adds the last character to the results
50 }
51 }
52 return results;//returns the result
53 }
54}

Callers

nothing calls this directly

Calls 2

GetNumMethod · 0.95
lengthMethod · 0.80

Tested by

no test coverage detected