📎 Webclip
Roman To Numbers To Roman in C#
The page covers Leetcode problems 13 and 12. For Roman to Integer, it uses a dictionary of Roman symbols and their values, then adds or subtracts based on whether the next symbol is larger than the current one. It notes the need to handle the last element to avoid an index error.
For Integer to Roman, it presents two approaches. The first uses a dictionary and handles the six subtractive cases, IV, IX, XL, XC, CD, CM, by traversing values from large to small and shrinking the number until it reaches zero. The second uses two arrays, one for Roman letters and one for numbers, with time complexity O(n) and space complexity O(n + m).
Reading notes#
- Roman to Integer converts a Roman numeral string into its corresponding integer value.
- The Roman to Integer logic compares each symbol with the next one and subtracts when the next value is greater.
- The example
MCMXCIVconverts to1994. - A dictionary stores Roman symbols and their integer values.
- The code must handle the last element to avoid an Array IndexOutOfBoundsException.
- Integer to Roman is the reverse problem.
- One approach uses a dictionary and includes the subtractive pairs
IV, IX, XL, XC, CD, CM. - This approach traverses values from large to small and repeats each value while the number remains large enough.
- The example
350becomesCCCL. - The first Integer to Roman approach has time complexity O(n * m) and space complexity O(n).
- The second approach uses two arrays, one for Roman letters and one for numbers.
- The second approach has time complexity O(n) and space complexity O(n + m).
