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

  • 相关阅读:
    2-5 Flutter开发环境与Android开发环境设置详解(Windows)
    2-3 Flutter开发环境与iOS开发环境设置(Mac)
    2-1 本章作业&2-2 开发系统与工具选择
    ASP.NET Core会议管理平台实战_4、参数校验、操作结果封装,注册参数配置
    ASP.NET Core会议管理平台实战_3、认证、授权表迁移
    29.镜像容器与仓库
    27.集成EFCore配置Client和API
    26.OpenIdConnect获取用户信息的两种方式
    25.ProfileService实现(调试)
    24.集成ASP.NETCore Identity
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4431088.html
Copyright © 2011-2022 走看看