Java 将罗马数字转换为介于1到3999之间的十进制数
罗马数字是一种基于古罗马符号体系的数字表示方法。字母M、D、C、L、X、V和I分别代表1000、500、100、50、10、5和1。在下面的部分中,将讨论所有主要符号。 在这个问题中,我们给定一个罗马数字的字符串,我们的任务是将罗马数字转换为介于1到3999之间的十进制数。
让我们通过以下的示例和解释来更好地理解这个问题。
输入1
str = "MCMIX"
输出1
1909
解释
M是罗马数字代表1000,
CM是罗马数字代表900,
IX是罗马数字代表9。
输入2
str = "IV"
输出结果
2
说明
IV是罗马数字4的表示方式。
输入3
str = "VI"
输出3
6
SYMBOL | VALUE |
---|---|
I | 1 |
IV | 4 |
IX | 9 |
V | 5 |
X | 10 |
XL | 40 |
XC | 90 |
L | 50 |
C | 100 |
CD | 400 |
CM | 900 |
D | 500 |
M | 1000 |
方法
我们已经看到了给定的罗马数字字符串的示例,让我们来看一下方法。
根据观察,罗马数字的符号按照降序来表示数字(例如C排在X之前等等)。然而,在某些情况下,它也使用减法表示法来避免连续重复4个字符(例如CCCC或IIII):
- I在V或X之前表示一个较小的数字。
例如:4 = IV(比五小1)
9 = IX(比十小1)
- X在L或C之前表示十个较小的数字。
例如:40 = XL(比五十小十)
90 = XC(比一百小10)
- C在D和M之前表示一个较小的数字。
例如:400 = CD(比五百小百)
900 = CM(比一千小百)
让我们来看下面的代码,以更好地理解上述方法。
示例
下面是一个Java程序,用于将罗马数字转换为介于1到3999之间的十进制数。
import java.util.*;
// Create a class for initializing the function to return the Roman symbol's value.
public class Solution{
// This function is created to return a Roman symbol's value.
int romanValue(char ch) {
if(ch=='I')
return 1;
else if(ch=='V')
return 5;
else if(ch=='X')
return 10;
else if(ch=='L')
return 50;
else if(ch=='C')
return 100;
else if(ch=='D')
return 500;
else if(ch=='M')
return 1000;
return -1;
}
// This function is created for the conversion of Roman numerals to decimal numerals
int convertRomanToDecimal(String str) {
// Initialize decimal value
int decVal = 0;
int n = str.length(); // Getting the size of the string
for (int i = 0; i < n; i++) {
// check if i+1 charchter exist and getting value of roman symbol str[i] and str[i+1]
if (i + 1 < n && romanValue(str.charAt(i)) < romanValue(str.charAt(i + 1))) {
//subtract the current value from the next value and add the decVal variable
decVal = decVal + romanValue(str.charAt(i + 1)) - romanValue(str.charAt(i));
i++; // Increment the index of the string to point to the next char
}
// If i+1 char not exist
else {
decVal = decVal + romanValue(str.charAt(i)); // add the first char value
}
}
return decVal; // Return decimal value
}
public static void main(String args[]) {
Solution ob = new Solution();
String str = "MCMIX"; // Given string
System.out.println("Roman Numeral: " + str);
// Print the decimal form and call the function of conversion
System.out.println("The decimal Numeral form of the Roman Numeral" + " is " + ob.convertRomanToDecimal(str));
}
}
输出
Roman Numeral: MCMIX
The decimal Numeral form of the Roman Numeral is 1909
时间和空间复杂度
上述代码的时间复杂度是O(N),因为只需要对字符串进行一次遍历。其中N是给定罗马数字字符串的长度。由于没有使用额外的空间,上述代码的空间复杂度为O(1)。
使用哈希表的另一种解决方案
示例
import java.util.Map;
import java.util.HashMap;
public class Solution {
public static final Map<Character,
Integer> romanValue = new HashMap<Character,
Integer>() {
{
put ( 'M', 1000 );
put ( 'D', 500 );
put ( 'C', 100 );
put ( 'L', 50 );
put ( 'X', 10 );
put ( 'V', 5 );
put ( 'I', 1 );
}
};
// Function is created for conversion of the Roman numeral to decimal numeral
private static int convertRomanToDecimal(String str) {
// Initialize decimal value
int decVal = 0;
for (int i = 0; i < str.length(); i++) {
// store numeric value of roman symbol str[i]
int first = romanValue.get(str.charAt(i));
// check if i+1 charchter exist or not
if (i + 1 < str.length()) {
// store numeric value of roman symbol str[i+1]
int second = romanValue.get(str.charAt(i + 1));
// check which value is greater first or second
if (first <= second) {
// if first value <= second add first value to variable decVal
decVal = decVal + first;
} else {
// Value of first char is less than to the the second char
decVal = decVal + second - first;
i++; // Increment the index of string to point to next char
}
}
// If i+1 char not exist
else {
decVal = decVal + first; // add the first char value
}
}
return decVal; // Return decimal value
}
public static void main(String args[]) {
String str = "MMMIX"; // Given string
System.out.println("Roman Numeral: " + str);
// print the decimal form and call function of conversion
System.out.println("Decimal Numeral form of Roman Numeral" +
" is " + convertRomanToDecimal(str));
}
}
输出
Roman Numeral: MMMIX
Decimal Numeral form of Roman Numeral is 3009
结论
在本教程中,我们实现了一个Java程序,用于将罗马数字转换为1至3999之间的十进制数。我们实现了两种解决方案:一种是使用普通函数,另一种是使用哈希映射函数。