LeetCode 0012 整数转罗马数字(Java)

写在前面:
记录LeetCode练习过程,有自己的解决方法,也有借鉴了网上后实现的方法。方法不一定最优秀,但都是实践可用的。一些解释和理解写在程序起始的注释中。若有什么不当之处,欢迎留言交流,大家一起学习。

import java.util.HashMap;
import java.util.Map;

/**
 * LeetCode 0012 整数转罗马数字
 * author: MammothKan
 * time: 2020/5/2
 * 题目描述:https://leetcode-cn.com/problems/integer-to-roman/
 * 解题思路:
 *      在程序开始之初,构建罗马数字与阿拉伯数字的对应关系,方法一借助了hashMap的键值对形式,方法二借助了两个数组。
 *      方法一:通过取余法,获得阿拉伯数字的每一位,再针对每一位转化为罗马数字。程序中每次都取阿拉伯数字的最后一位,再借助一个变量e标记当前位是百位or千位or其他...
 *      方法二:采用贪心的思想,每一次取出可以转化的最大数,做转换。所以对程序起始时的两个数组有顺序要求,此处采用的降序。
 *      以上两个方法,方法一从右往左按位处理,leetcode消耗时长11ms;方法二采用贪心策略,先处理最大可处理的值,leetcode消耗时长为5ms。
 */

public class L0012整数转罗马数字 {

    /**
     * 方法一
     * @param num
     * @return
     */
    public String intToRoman(int num) {
        String romanInt = "";
        Map<Integer, String> romanIntMap = new HashMap(){{
            put(1, "I");
            put(4, "IV");
            put(5, "V");
            put(9, "IX");
            put(10, "X");
            put(40, "XL");
            put(50, "L");
            put(90, "XC");
            put(100, "C");
            put(400, "CD");
            put(500, "D");
            put(900, "CM");
            put(1000, "M");
        }};
        int e = 1;
        while (num != 0) {
            String thisRomanInt = "";
            int thisNum = num % 10;
            if ((thisRomanInt = romanIntMap.get(thisNum * e)) != null) { }
            else if (thisNum < 4) {
                thisRomanInt = "";
                for (int i=0; i<thisNum; i++) {
                    thisRomanInt += romanIntMap.get(e);
                }
            }
            else {
                thisRomanInt = romanIntMap.get(5*e);
                for (int i=0; i<thisNum-5; i++) {
                    thisRomanInt += romanIntMap.get(e);
                }
            }
            romanInt = thisRomanInt + romanInt;
            num /= 10;
            e *= 10;
        }
        return romanInt;
    }

    /**
     * 方法二
     * @param num
     */
    public static String intToRoman2(int num) {
        int[] numArray = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] romanIntArray = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int index = 0;
        StringBuffer romanIntSB = new StringBuffer();
        while (num > 0){
            if ((num - numArray[index++]) >= 0) {
                romanIntSB.append(romanIntArray[--index]);
                num -= numArray[index];
            }
        }
        return romanIntSB.toString();
    }

    public static void main(String[] args) {
        System.out.println(new L0012整数转罗马数字().intToRoman(1994));
        System.out.println(new L0012整数转罗马数字().intToRoman2(1994));
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值