LeetCode in C# | 13. Roman to Integer

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

that's just what you need

public class Solution {
public int RomanToInt(string s) {
Dictionary<char, int> romanValues = new Dictionary<char, int> {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};

int result = 0;
int prevValue = 0;

for (int i = s.Length - 1; i >= 0; i--) {
int value = romanValues[s[i]];
if (value < prevValue)
result -= value;
else
result += value;
prevValue = value;
}

return result;
}
}

cupck
Автор

This is almost more like a linguistic challenge than a coding challenge, biggest hurdle is remembering all the rules

LaughingMan