LeetCode-13. 罗马数字转整数

题目的规则里排除了IL和IM的情况,那整个罗马数字转换的时候就是如果前一个数字比后一个数字小那就做减法

解法一:

用HashMap把字母对应的数字保存下来,之后从前往后遍历,判断做加法还是减法

class Solution {
    
    public static HashMap<String,Integer> map = new HashMap();

    static {
        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[] charArray = s.toCharArray();
        int now = 0;
        int after = 1;
        int sum = 0;
        while (now < charArray.length){
            sum += doChange(charArray,now,after);
            now ++;
            after ++;
        }
        return sum;

    }

    public int doChange(char[] charArray, int now, int after){
        int nowValue = map.get(String.valueOf(charArray[now]));
        if (after >= charArray.length){
            return nowValue;
        }
        int afterValue = map.get(String.valueOf(charArray[after]));
        if (nowValue >= afterValue){
            return nowValue;
        }
        return - nowValue;
    }

    
}

解法二:

用switch语句替换掉HashMap,可以发现小数量数据在查找时,map并没有优势

class Solution {

    public int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int now = 0;
        int after = 1;
        int sum = 0;
        while (now < charArray.length){
            sum += doChange(charArray,now,after);
            now ++;
            after ++;
        }
        return sum;

    }

    public int doChange(char[] charArray, int now, int after){
        int nowValue = getValue(charArray[now]);
        if (after >= charArray.length){
            return nowValue;
        }
        int afterValue = getValue(charArray[after]);
        if (nowValue >= afterValue){
            return nowValue;
        }
        return - nowValue;
    }

    public int getValue(char c){
        switch(c){
            case 'I':
              return 1;
            case 'V':
              return 5;
            case 'X':
              return 10;
            case 'L':
              return 50;
            case 'C':
              return 100;
            case 'D':
              return 500;
            case 'M':
              return 1000;
            default:
              throw new RuntimeException();
        }
    }

    
}

解法三:

因为题目限制了最大数,所以我们可以把特殊情况列举出来,比如IV转换成a,a代表4

class Solution {
    public int romanToInt(String s) {
        s = s.replace("IV","a");
        s = s.replace("IX","b");
        s = s.replace("XL","c");
        s = s.replace("XC","d");
        s = s.replace("CD","e");
        s = s.replace("CM","f");
        
        int result = 0;
        for (int i=0; i<s.length(); i++) {
            result += which(s.charAt(i));
        }
        return result;
    }

    public int which(char ch) {
        switch(ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            case 'a': return 4;
            case 'b': return 9;
            case 'c': return 40;
            case 'd': return 90;
            case 'e': return 400;
            case 'f': return 900;
        }
        return 0;
    }
}

前期replace耗时比较多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值