12. Integer to Roman

题目类型:

HashMap,字符串

 beat:87%

题意:

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

罗马数字的写法:

![罗马数字的写法](https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1515832886&di=49daa22fa4c13b30fb3550fa54e1e342&src=http://p12.qhimg.com/t010e6ed1b8c9f8e2cf.png)

罗马数字的规则:

  1. 罗马数字共有7个,
基本字符相应的阿拉伯数字
I1
V5
X10
L50
C100
D500
M1000
2. 右加左减: - 在一个较大的罗马数字的右边记上一个较小的罗马数字,表示大数字加小数字。 - 在一个较大的数字的左边记上一个较小的罗马数字,表示大数字减小数字 3. 左减数字规则: - 左减不能跨位,位于较大数字左边的较小数字只能为该数字的前面一个数字:比如,99不可以用IC表示,而是用XCIX表示。(C前面一个数字位L) - 左减数字不能超过一位,比如8写成VIII,而非IIX - 右加数字不能超过三位,比如十四写成XIV,而非XIIII - 同样单位只能出现3次,如40不能表示为XXXX,而要表示为XL 4. 一个数重复几次,就表示这个数的几倍

我的思路:

效率:52%

步骤:

  1. 已知输入为1到3999,特殊数字为0 9 4 6 1
  2. 对从高位到低位的每一位数,如果为4/9就用左减,如果为5就用对应的5的表示方法,如果为1-3就用重复的三个一,如果为6-8就用五加一
  3. 例如3999,最高位为3,用重复的三个1000MMM,第二位为900,用1000-100:CM,第三位为90,用100-10:XC,第四位为9,用10-1,IX,拼接:MMMCMXCIX
    public String intToRoman(int num) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "I");
        map.put(5, "V");
        map.put(10, "X");
        map.put(50, "L");
        map.put(100, "C");
        map.put(500, "D");
        map.put(1000, "M");
        StringBuilder res = new StringBuilder();
        int len = 1;
        while (num / len >= 10) {
            len *= 10;
        }
        while (num != 0) {
            int cur = num/ len;
            String roman = map.get(len);
            if (cur == 4 || cur == 9) {
                res.append(roman);
                res.append(map.get((cur + 1) * len));
            }
            else{
                if (cur >= 5) {
                    res.append(map.get(5 * len));
                    cur = cur - 5;
                }
                for(int i = 0; i < cur; i++){
                    res.append(roman);
                }
            }
            num %= len;
            len /= 10;

        }
        return res.toString();
    }

方法二:不断减小的num

  • 两个数组,分别存放阿拉伯数字及对应的罗马数字(包括0 9 5 4 1)
  • for循环每一个阿拉伯数字,如果x - nums[i] >= 0,那么添加nums[i]对应的罗马数字,x减去nums[i]
  • 循环直至x为0
class Solution {
    //方法二:不断减
    int[] nums = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] strings = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    public String intToRoman(int num) {
        StringBuilder res = new StringBuilder();
        for(int i = 0; i < nums.length; i++){
            while (num >= nums[i]) {
                res.append(strings[i]);
                num -= nums[i];
            }
        }
        return res.toString();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值