leet-code/src/main/java/leetcode/editor/cn/RomanToInteger.java
huangge1199@hotmail.com 3303fa2d7a 13:罗马数字转整数
2021-05-16 15:49:53 +08:00

114 lines
3.2 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//罗马数字包含以下七种字符: I V X LCD 和 M。
//
//
//字符 数值
//I 1
//V 5
//X 10
//L 50
//C 100
//D 500
//M 1000
//
// 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + I
//I 。
//
// 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5
// 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况
//
//
// I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
// X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
// C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
//
//
// 给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
//
//
//
// 示例 1:
//
//
//输入: "III"
//输出: 3
//
// 示例 2:
//
//
//输入: "IV"
//输出: 4
//
// 示例 3:
//
//
//输入: "IX"
//输出: 9
//
// 示例 4:
//
//
//输入: "LVIII"
//输出: 58
//解释: L = 50, V= 5, III = 3.
//
//
// 示例 5:
//
//
//输入: "MCMXCIV"
//输出: 1994
//解释: M = 1000, CM = 900, XC = 90, IV = 4.
//
//
//
// 提示:
//
//
// 1 <= s.length <= 15
// s 仅含字符 ('I', 'V', 'X', 'L', 'C', 'D', 'M')
// 题目数据保证 s 是一个有效的罗马数字,且表示整数在范围 [1, 3999] 内
// 题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。
// IL 和 IM 这样的例子并不符合题目要求49 应该写作 XLIX999 应该写作 CMXCIX 。
// 关于罗马数字的详尽书写规则,可以参考 罗马数字 - Mathematics 。
//
// Related Topics 数学 字符串
// 👍 1352 👎 0
package leetcode.editor.cn;
//13:罗马数字转整数
public class RomanToInteger{
public static void main(String[] args) {
//测试代码
Solution solution = new RomanToInteger().new Solution();
//3
System.out.println(solution.romanToInt("III"));
//4
System.out.println(solution.romanToInt("IV"));
//9
System.out.println(solution.romanToInt("IX"));
//58
System.out.println(solution.romanToInt("LVIII"));
//1994
System.out.println(solution.romanToInt("MCMXCIV"));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int romanToInt(String s) {
int[] values = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] labels = new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int num = 0;
for (int i = 0; i < labels.length && s.length() > 0; i++) {
int value = values[i];
String label = labels[i];
while (s.startsWith(label)) {
s = s.substring(label.length());
num += value;
}
}
return num;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}