每天一个小程序(三)--- Roman To Integer

学习 同时被 3 个专栏收录
31 篇文章 0 订阅
21 篇文章 0 订阅
14 篇文章 0 订阅

RomanToInteger

おーす

此方和小镜

前言:

あらあら,昨天一不留神就忘了,不过没(没)关(人)系(看),今天是一个将罗马数字转为阿拉伯数字的问题,代码见:github


package math;

/**
 * @author BlackSugar
 * @date 2019/4/16
 * Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
 * <p>
 * Symbol       Value
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 * For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
 * <p>
 * Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
 * <p>
 * I can be placed before V (5) and X (10) to make 4 and 9.
 * X can be placed before L (50) and C (100) to make 40 and 90.
 * C can be placed before D (500) and M (1000) to make 400 and 900.
 * Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
 * <p>
 * Example 1:
 * <p>
 * Input: "III"
 * Output: 3
 * Example 2:
 * <p>
 * Input: "IV"
 * Output: 4
 * Example 3:
 * <p>
 * Input: "IX"
 * Output: 9
 * Example 4:
 * <p>
 * Input: "LVIII"
 * Output: 58
 * Explanation: L = 50, V= 5, III = 3.
 * Example 5:
 * <p>
 * Input: "MCMXCIV"
 * Output: 1994
 * Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
 */
public class RomanToInteger {
    /**
     * 罗马数字转为阿拉伯数字
     * Symbol       Value
     * I             1
     * V             5
     * X             10
     * L             50
     * C             100
     * D             500
     * M             1000
     * 思路:由于罗马数字是从大到小排列,若前一位小于后一位则为特殊数字
     * 把所有字符代表的值加起来再剔除六个特殊的,减少的值为特殊字符前一个字符*2
     *
     * @param s
     * @return
     */
    public int romanToInt(String s) {
        if (null == s || s.length() == 0) {
            return 0;
        }
        int sum = getInteger(s.charAt(0)), pre = getInteger(s.charAt(0));
        for (int i = 1; i < s.length(); i++) {
            int next = getInteger(s.charAt(i));
            sum += next;
            //if pre_value is less than next_value: IV,IX,XL,XC,CD,CM
            if (pre < next) {
                sum -= pre * 2;
            }
            pre = next;
        }
        return sum;
    }

    private int getInteger(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:
                return 0;
        }
    }


    public static void main(String[] args) {
        System.out.println(new RomanToInteger().romanToInt("XII"));
    }
}

总结:

这题没什么难度,直接将出现的字符按照对应的值相加即可。稍微麻烦的特殊数字,只要注意到特殊数字是小的字符在大的前面,然后指定一个慢一步的指针做相应处理即可

1、时间复杂度O(n)
2、空间复杂度O(1)

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值