• [LeetCode#12] Roman to Integer


    Problem:

    Given an integer, convert it to a roman numeral.

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

    Analysis:

    This problem is trivial, if you really understand the principle to construct a Roman number.
    It is really really very very simple!
    Note: Keep in mind!!! The character used in roman number include digit weight, but for numerial system it based on position. Thus you should find a way to convert the digit weight(in positional representation) into digital weight(through character combination)
    
    for (int i = 0; i < 4; i++) {
        int digit_weight = (int)Math.pow(10, 3-i);
        int digit = num / digit_weight;
        switch (digit) {
            ...
        }
    }

    Solution:

    public class Solution {
        public String intToRoman(int num) {
            if (num <= 0 || num > 3999)
                throw new IllegalArgumentException("The passed in argument is illegal");
            StringBuffer ret = new StringBuffer();
            HashMap<Integer, Character> map = new HashMap<Integer, Character> ();
            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');
            for (int i = 0; i < 4; i++) {
                int digit_weight = (int)Math.pow(10, 3-i);
                int digit = num / digit_weight;
                switch (digit) {
                    case 1 : 
                        ret.append(map.get(digit_weight));
                        break;
                    case 2 : 
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight));
                        break;
                    case 3:
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight));
                        break;
                    case 4:
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight * 5));
                        break;
                    case 5:
                        ret.append(map.get(digit_weight * 5));
                        break;
                    case 6:
                        ret.append(map.get(digit_weight * 5));
                        ret.append(map.get(digit_weight));
                        break;
                    case 7:
                        ret.append(map.get(digit_weight * 5));
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight));
                        break;
                    case 8:
                        ret.append(map.get(digit_weight * 5));
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight));
                        break;
                    case 9:
                        ret.append(map.get(digit_weight));
                        ret.append(map.get(digit_weight * 10));
                        break;
                }
                num = num % digit_weight;
            }
            return ret.toString();
        }
    }
  • 相关阅读:
    WPF样式统一之DevExpress设置窗体,控件为Office风格
    vs报错 "多步操作产生错误。请检查每一步的状态值"
    WPF属性之理解附加属性
    WPF国际化方式1之资源文件
    EntityFramework经典数据访问层基类——增删改查
    一个sh脚本 同时运行 多个sh脚本
    安装OpenIMSCore的SIP测试客户端 utcimsclient
    No module named 'paddle.fluid'
    “/usr/local/lib/libosipparser2.so.7: could not read symbols: Invalid operation” 异常解决
    把ubuntu自带的高gcc版本降到低版本(如gcc 3.4)的方法
  • 原文地址:https://www.cnblogs.com/airwindow/p/4778111.html
Copyright © 2020-2023  润新知