LeetCode第13题--Roman to Integer(Java实现)

LeetCode第13题–Roman to Integer(Java实现)

原题

Given a roman numeral, convert it to an integer.

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

解题思路

先弄明白什么是罗马数字:

这7个符号与10进制阿拉伯数字的对应关系是:

I=1;X=10;C=100;M=1000; 

V=5;L=50;D=500;

罗马数字编码规则如下:

1.编码最短,左减右加;
2.加减不能跨“数量级”;
3.减不过1,加不过3;    
算法思路

找到字符串中最大的字符,找到其对应的数字,递归地做左减右加。

算法实现


    public class Solution {
    private static final Map<Character, Integer> map = new HashMap<Character, Integer>();
    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) {
        return romanToIntSelf(s, 0, s.length());

    }

    private int romanToIntSelf(String s, int start, int end) {
        if (start >= end) return 0;
          //特别注意:result就在递归,第一个递归函数的result用来回收左右递归所返回的值,并累加中间的值。
        int result = 0;
        int maxIndex = findMaxIndex(s, start, end);
        char max = s.charAt(maxIndex);
        result += map.get(max);
        result += romanToIntSelf(s, maxIndex + 1, end);
        result -= romanToIntSelf(s, start, maxIndex);
        return result;
    }

    /**
     * 找到字符串中表示最大的罗马数字支付串所在的位置
     *
     * @param s
     * @param start inclusive
     * @param end   exclusive
     */
    private int findMaxIndex(String s, int start, int end) {
        char temp = s.charAt(start);
        int result = start;
        for (int i = start + 1; i < end; i++) {
            if (bigger(s.charAt(i), temp)) {
                result = i;
                temp = s.charAt(i);
            }

        }
        return result;
    }

    /**
     * is c1 in romandigit bigger than c2
     *
     * @return
     */
    private boolean bigger(char c1, char c2) {
        return map.get(c1) > map.get(c2);
    }

    class RomanDigit implements Comparable<RomanDigit> {

        public RomanDigit() {
        }

        @Override
        public int compareTo(RomanDigit r) {
            return 0;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "XCIX"; //99
        System.out.println(solution.findMaxIndex(s, 0, s.length()));
        System.out.println(solution.romanToInt(s));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值