➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Roman To Integer

image image

The key intuition lies in the fact that in Roman Numerals, when a smaller value appears before a larger value, it represents subtraction, while when a smaller value appears after or equal to a larger value, it represents addition.

Explanation:
    ans -= m[s[i]];
    ans -= m['I'];
    ans -= 1;
    ans becomes -1.
    ans += m[s[i]];
    ans += m['X'];
    ans += 10;
    ans becomes 9.

##### Taking XI as the test case

    ans += m[s[i]];
    ans += m['X'];
    ans += 10;
    ans becomes 10.
    ans += m[s[i]];
    ans += m['I'];
    ans += 1;
    ans becomes 11.

Solve Script: link image