-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroman_to_integer.dart
52 lines (41 loc) · 958 Bytes
/
roman_to_integer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
int romanToInt(String s) {
List<int> numbers = List<int>.filled(s.length, 0);
int sum = 0;
String soo ='kjfkdjf';
for (int i = 0; i < s.length; i++) {
switch (s[i]) {
case 'I':
numbers[i] = 1;
break;
case 'V':
numbers[i] = 5;
break;
case 'X':
numbers[i] = 10;
break;
case 'L':
numbers[i] = 50;
break;
case 'C':
numbers[i] = 100;
break;
case 'D':
numbers[i] = 500;
break;
case 'M':
numbers[i] = 1000;
break;
}
}
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] < numbers[i + 1]) {
sum -= numbers[i];
} else {
sum += numbers[i];
}
}
return sum + numbers[numbers.length - 1];
}
}
//problme: https://leetcode.com/problems/roman-to-integer/