LeetCode-12. Integer to Roman-13. Roman to Integer

来源:互联网 发布:香水知识 知乎 编辑:程序博客网 时间:2024/05/23 00:04

12. Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

将一个int型的数字转化为罗马数字,范围在1-3999。下面是罗马数字的介绍及基本规则:
罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。记数的方法:
- 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3
- 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12
- 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9

注意点:
- 基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个,放在大数的左边只能用一个
- 不能把基本数字 V 、L 、D中的任何一个作为小数放在大数的左边采用相减的方法构成数目,放在大数的右边采用相加的方式构成数目时只能使用一个
- V 和 X 左边的小数字只能用 Ⅰ
- L 和 C 左边的小数字只能用 X
- D 和 M 左边的小数字只能用 C

解题思路

根据上面的规则和注意点可以看出,罗马字符及其组合能代表的数字有:

阿拉伯数字 罗马数字 1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I
package solutions._12;/** * 12. Integer to Roman */class Solution {    public String intToRoman(int num) {        /**         *     "M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I"         *    1000 900  500 400  100  90  50  40   10   9    5   4    1         */        StringBuilder sb = new StringBuilder();        String symbol[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};        int value[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};        for (int i = 0; num != 0; i++) {            while (num >= value[i]) {                num -= value[i];                sb.append(symbol[i]);            }        }        return sb.toString();    }    public String intToRoman2(int num) {        //1000  2000  3000        String M[] = {"", "M", "MM", "MMM"};        //100 200 300 400 500 600 700 800 900        String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};        //10 20 30 40 50 60 70 80 90        String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};        //1 2 3 4 5 6 7 8 9        String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};        return M[num / 1000] +                C[(num % 1000) / 100] +                X[(num % 100) / 10] +                I[num % 10];    }    public static void main(String[] args) {        Solution solution = new Solution();        for (int i = 1; i <= 1000; i++) {            System.out.println(i + ":" + solution.intToRoman(i));        }    }}

13. Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

package solutions._13;import java.util.HashMap;import java.util.Map;/** * 13. Roman to Integer */public class Solution {    private Map<Character, Integer> map = new HashMap<>();    public Solution() {        map.put('I', 1);        map.put('V', 5);        map.put('X', 10);        map.put('L', 50);        map.put('C', 100);        map.put('D', 500);        map.put('M', 1000);    }    public int romanToInt(String s) {        char[] arr = s.toCharArray();        int result = 0;        for (int i = 0; i < arr.length; i++) {            int val = map.get(arr[i]);            if (i == arr.length - 1 || map.get(arr[i + 1]) <= map.get(arr[i])) {                result += val;            } else {                result -= val;            }        }        return result;    }    public static void main(String[] args) {        Solution solution = new Solution();        System.out.println(solution.romanToInt("VIII"));    }}