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();
        }
    }
  • 相关阅读:
    前端 CSS 基础
    前端 HTML基础
    前端 JavaScript基础
    GoldenGate 复制进程报错"OGG-01296 Error mapping",丢弃文件报错“Mapping problem with delete record (target format)”,且实际条目存在
    SaltStack 与 Python 程序的结合
    SUSE-11 本地 zypper 配置
    Centos-7 + Docker-1.12 中 devicemapper + direct_lvm 的 Docker 存储配置
    Docker-v17 的层级(layer)概念
    Oracle-11g 中使用表空间透明数据加密(TDE)
    SaltStack 的远程执行机制
  • 原文地址:https://www.cnblogs.com/airwindow/p/4778111.html
Copyright © 2011-2022 走看看