zoukankan      html  css  js  c++  java
  • [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();
        }
    }
  • 相关阅读:
    SelectionKey理解
    redis3.0.3集群搭建
    Centos6.5环境下安装SVN 整合Apache+SSL
    没有注册类 。已加载,但找不到入口点 DllRegisterServer
    今日立秋
    35+开启忙而有序的日子
    jmeter的常用函数
    jmeter之java请求
    生成Webservice客户端的4种方法
    Pytest高级进阶之Fixture
  • 原文地址:https://www.cnblogs.com/airwindow/p/4778111.html
Copyright © 2011-2022 走看看