Roman to Integer — Clean, Fast, and Functional Approaches Compared
🧩 Problem: Roman to Integer (LeetCode 13) Convert a string representing a Roman numeral into its integer value. Roman numerals are typically written from largest to smallest (left to right), except for six well-known subtractive cases: I before V or X → 4, 9 X before L or C → 40, 90 C before D or M → 400, 900 ✅ Clean Idiomatic Solution (One-Pass) class Solution: SYMBOLS = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } def romanToInt(self, s: str) -> int: total = 0 prev_value = 0 for c in s: value = self.SYMBOLS[c] total += value - 2 * prev_value if value > prev_value else value prev_value = value return total Single loop Detects subtractive patterns with value > prev_value Clean inline logic ✅ Runtime: 3 ms (faster than 79.17%) ✅ Memory: 17.6 MB (less than 90.19%) ...