📘 LeetCode 13: Roman to Integer
📌 LeetCode: 13.roman-to-integer
📋 Problem Statement
title: “LeetCode 13: Roman to Integer — Engineering & Algorithmic Solution Note” problem_number: 13 problem_name: “Roman to Integer” difficulty: “Easy” category: “dsa_solutions” tags: [leetcode-13, roman-to-integer, hash-map, string, math, csharp, dotnet9, dsa, problem-solving, leetcode, learning-note]
📘 LeetCode 13: Roman to Integer — Engineering & Algorithmic Solution Note
📌 Connected Resource Graph
- 🟢 Primary Theory Note: 00_overview_hash_tables_dictionary_hashmap.md
- 🟡 String Algorithms: 00_overview_string_manipulation.md
- 📂 Local Project Companion Files:
- 💻
Program.cs - ⚡
complexity.md - 🚨
tips.md
- 💻
1. 🎯 Problem Statement & Constraints
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
| Symbol | Value |
|---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Usually, Roman numerals are written largest to smallest from left to right. However, when a smaller numeral appears before a larger one, subtraction applies:
IV= 4,IX= 9XL= 40,XC= 90CD= 400,CM= 900
Given a roman numeral s, convert it to an integer.
Constraints
scontains only the characters('I', 'V', 'X', 'L', 'C', 'D', 'M').- It is guaranteed that
sis a valid roman numeral in the range .
2. 💡 Optimal Implementation (C#)
public static int RomanToInt(string s)
{
int total = 0;
int prevValue = 0;
// Traverse from right to left for simple addition / subtraction logic
for (int i = s.Length - 1; i >= 0; i--)
{
int currValue = s[i] switch
{
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
_ => 0
};
if (currValue < prevValue)
{
total -= currValue;
}
else
{
total += currValue;
prevValue = currValue;
}
}
return total;
}