logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
13. Roman to Integer14. Longest Common Prefix20. Valid Parentheses26. Remove Duplicates from Sorted Array35. Search Insert Position69. Sqrt(x)70. Climbing Stairs88. Merge Sorted Array (In-Place)104. Maximum Depth of Binary Tree118. Pascal's Triangle121. Best Time to Buy and Sell Stock205. Isomorphic Strings242. Valid Anagram283. Move Zeroes345. Reverse Vowels of a String605. Can Place Flowers704. Binary Search796. Rotate String1071. Greatest Common Divisor of Strings1299. Replace Elements with Greatest Element on Right Side1431. Kids With the Greatest Number of Candies1614. Maximum Nesting Depth of the Parentheses1752. Check if Array Is Sorted and Rotated1768. Merge Strings Alternately1903. Largest Odd Number in String2965. Find Missing and Repeated Values
Code Scratch/leetcode/easy

13. Roman to Integer

2 min read
On this pageIntuition
IntuitionComplexityCode

# Intuition

Roman numerals are usually written from largest to smallest (left to right).
However, when a smaller numeral appears before a larger one, we subtract it (e.g., IV = 5 - 1 = 4).

To handle this, we iterate through the Roman string:

  • Normally, we add each numeral’s value.
  • But if the current numeral is greater than the previous, it means we’ve already added a smaller numeral that should’ve been subtracted, so we adjust using:
    value -= 2 * previous.

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(N)\text{O}(N)O(N)

# Code

public int romanToInt(String romanString) {
    // Step 1: Create a lookup array to map Roman characters to their integer values
    int[] romanToValue = new int[26]; // Covers A-Z
    romanToValue['I' - 'A'] = 1;
    romanToValue['V' - 'A'] = 5;
    romanToValue['X' - 'A'] = 10;
    romanToValue['L' - 'A'] = 50;
    romanToValue['C' - 'A'] = 100;
    romanToValue['D' - 'A'] = 500;
    romanToValue['M' - 'A'] = 1000;
 
    int totalValue = 0;         // Final result
    int previousValue = -1;     // Holds the value of the previous Roman character
 
    // Step 2: Traverse the Roman numeral string
    for (int i = 0; i < romanString.length(); ++i) {
        int currentValue = romanToValue[romanString.charAt(i) - 'A']; // Get int value of current Roman char
        totalValue += currentValue; // Add it by default
 
        // Step 3: If previous character was smaller (like I before V), subtract double of it
        if (previousValue != -1 && previousValue < currentValue) {
            totalValue -= 2 * previousValue;
        }
 
        previousValue = currentValue; // Update for next iteration
    }
 
    return totalValue; // Return the final integer value
}
←PreviousTable Of ContentsNext→14. Longest Common Prefix

On this page

IntuitionComplexityCode