zoukankan      html  css  js  c++  java
  • 12. Integer to Roman

    题目:

    Given an integer, convert it to a roman numeral.

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

    链接:    http://leetcode.com/problems/integer-to-roman/

    题解:

    数值计算的题目,需要了解罗马数字的规律。  Time Complexity - O(n), Space Complexity - O(n)

    public class Solution {
        public String intToRoman(int num) {   //Roman:   I = 1,  V = 5,   X = 10,   L = 50,   C = 100,  D = 500,  M = 1000 
            StringBuilder result = new StringBuilder();
            String [] symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};    
            int [] value = {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1};   
            for(int i = 0; num != 0; i++){
                while(num >= value[i]){
                    num -= value[i];
                    result.append(symbol[i]);
                }
            }
            return result.toString();
        }
    }

    二刷:

    I, IV, V, IX, X, XL, L, XC, C, CD, D, CM, M分别代表 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000

    Java:

    public class Solution {
        private final String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        private final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        
        public String intToRoman(int num) {
            if (num <= 0) {
                return "";
            }   
            StringBuilder sb = new StringBuilder();
            for (int i = 0; num > 0; i++) {
                while (num >= values[i]) {
                    num -= values[i];
                    sb.append(romans[i]);
                }    
            }
            return sb.toString();
        }
    }

    Python: 

    class Solution(object):
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
            numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
            res = ''
            for i in range(0, len(romans)):
                while num >= numbers[i]:
                    num -= numbers[i]
                    res += romans[i]
            return res

    三刷:

    Java:

    public class Solution {
        public String intToRoman(int num) {
            if (num <= 0) return "";
            String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
            int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
            StringBuilder sb = new StringBuilder();
            
            for (int i = 0; num > 0; i++) {
                while (num >= values[i]) {
                    num -= values[i];
                    sb.append(romans[i]);
                }    
            }
            return sb.toString();
        }
    }

    Reference:

    https://leetcode.com/discuss/49870/my-java-solution-easy-to-understand

  • 相关阅读:
    [转载]qemu-kvm安装配置
    Hadoop通过c语言API访问hdfs
    hadoop和hdfs环境搭建
    OpenCV installation for Ubuntu 12.04
    homework-01
    linux命令2
    压缩tar
    anaconda 安装opencv
    anconda安装第三方库
    开源代码
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4431088.html
Copyright © 2011-2022 走看看