You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
777 B
46 lines
777 B
package class28;
|
|
|
|
public class Problem_0013_RomanToInteger {
|
|
|
|
public static int romanToInt(String s) {
|
|
// C M X C I X
|
|
// 100 1000 10 100 1 10
|
|
int nums[] = new int[s.length()];
|
|
for (int i = 0; i < s.length(); i++) {
|
|
switch (s.charAt(i)) {
|
|
case 'M':
|
|
nums[i] = 1000;
|
|
break;
|
|
case 'D':
|
|
nums[i] = 500;
|
|
break;
|
|
case 'C':
|
|
nums[i] = 100;
|
|
break;
|
|
case 'L':
|
|
nums[i] = 50;
|
|
break;
|
|
case 'X':
|
|
nums[i] = 10;
|
|
break;
|
|
case 'V':
|
|
nums[i] = 5;
|
|
break;
|
|
case 'I':
|
|
nums[i] = 1;
|
|
break;
|
|
}
|
|
}
|
|
int sum = 0;
|
|
for (int i = 0; i < nums.length - 1; i++) {
|
|
if (nums[i] < nums[i + 1]) {
|
|
sum -= nums[i];
|
|
} else {
|
|
sum += nums[i];
|
|
}
|
|
}
|
|
return sum + nums[nums.length - 1];
|
|
}
|
|
|
|
}
|