public class Solution {
public int RomanToInt(string s) {
var numbers = new Dictionary<char, int>();
numbers.Add('I', 1);
numbers.Add('V', 5);
numbers.Add('X', 10);
numbers.Add('L', 50);
numbers.Add('C', 100);
numbers.Add('D', 500);
numbers.Add('M', 1000);
var numbersDiminished = new Dictionary<String, int>();
numbersDiminished.Add("IV", 4);
numbersDiminished.Add("IX", 9);
numbersDiminished.Add("XL", 40);
numbersDiminished.Add("XC", 90);
numbersDiminished.Add("CD", 400);
numbersDiminished.Add("CM", 900);
int result = 0;
int number = 0;
for (int i = 0; i < s.Length; i++) {
number = numbers[s[i]];
if ((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && i != s.Length - 1 ) {
string maybeDiminished = s.Substring(i, 2);
if (numbersDiminished.ContainsKey(maybeDiminished)) {
number = numbersDiminished[maybeDiminished];
i++;
}
}
result += number;
}
return result;
}
}
-
MY PROJECTS
-
Recent Posts
- Algo.Java.BinarySearch
- SQL. Conditional expression with count()
- Java.Algo.CumulativeApproach
- Algo.Java.BFS in BinaryTree
- Java.Jackson.Serialize object to json
- Algo. Traverse linked list
- Algo. Java. Remove-duplicates-from-sorted-array
- Java.Hibernate.SimpleExample
- Java.Jdbc.SimpleExample
- SQL. Calc the sum with case, example
- Mullvad – free the internet :)
- Junit. Jupiter
- Java. SpringBoot Example how to work with dateTime in Specification
- Java.SpringBoot.PopularAnnotations
- SpringBoot. Exception Management
- Java.Hibernate.JoinTableAnnotation
- SpringBoot.Making our first starter and autoconfiguration
- Spring. Creating main annotation to start business logic
- Spring.Reading from properties file
- Spring.How to define spring version inside springBoot ?
Categories
- Aptana
- Azure
- C#
- DataSnap
- DBExpress
- Delphi
- Delphi и сети
- Delphi. Язык программирования
- ExtJS
- FastReport
- FireDAC
- FireMonkey
- GIT
- ICS
- IDE
- IIS
- Indy
- InnoSetup
- javascript
- jQuery
- JSON
- LiveBindings
- MSHTML
- MySQL
- PHP
- REST
- Ribbons
- SMS
- SQL инструкции
- SVN
- TRichView
- UniGui
- WebBroker
- WinAPI
- Windows
- Алгоритмы
- Без рубрики
- Деревья
- Ищу ответ
- Компонентостроение
- Мои компоненты
- Начальный уровень
- Обработка исключений
- Парсинг
- Потоки(Threads)
- Регулярные выражения
- Тестирование приложений