12. LeetCode:整数转罗马数字

一. 问题描述

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。

字符          数值
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

ps:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
示例 1:
输入: 3
输出: "III"

示例 2:
输入: 4
输出: "IV"

示例 3:
输入: 9
输出: "IX"

示例 4:
输入: 58
输出: "LVIII"
解释: L = 50, V = 5, III = 3.

示例 5:
输入: 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.

二. 问题分析

对于给定的数据,我们需要从最高位向最低位判断即可,利用递归的方式重复调用就可以得到结果.

三. 代码书写

自己写的(low了点)

public String intToRoman(int num) {
        String result = "";
        int I = 1;
        int V = 5;
        int X = 10;
        int L = 50;
        int C = 100;
        int D = 500;
        int M = 1000;
        if (num >= M) {
            if (num < 4 * M) {
                for (int i=0; i<num/M; i++) {
                    result += "M";
                }
                int surplus = num % M;
                result += intToRoman(surplus);
            } else {
                return "输入超限";
            }
        } else if (num >= D){
            if (num < M - C) {
                result += "D";
                for (int i=0;i<(num-D)/C; i++) {
                    result += "C";
                }
            } else {
                result += "CM";
            }
            int surplus = num % C;
            result += intToRoman(surplus);
        } else if (num >= C){
            if (num < 4 * C) {
                for (int i=0; i<num/C; i++) {
                    result += "C";
                }
            } else {
                result += "CD";
            }
            int surplus = num % C;
            result += intToRoman(surplus);
        } else if (num >= L) {
            if (num < C - X) {
                result += "L";
                for (int i=0; i<(num-L)/X; i++) {
                    result += "X";
                }
            } else {
                result += "XC";
            }
            int surplus = num % X;
            result += intToRoman(surplus);
        } else if (num >= X) {
            if (num < 4 * X) {
                for (int i=0; i<num/X; i++) {
                    result += "X";
                }
            } else {
                result += "XL";
            }
            int surplus = num % X;
            result += intToRoman(surplus);
        } else if (num >= V) {
            if (num < X - I) {
                result += "V";
                for (int i=0; i<(num-V); i++) {
                    result += "I";
                }
            }else {
                result += "IX";
            }
        } else if (num >= I) {
            if (num < V -I) {
                for (int i=0;i<num; i++) {
                    result += "I";
                }
            } else {
                result += "IV";
            }
        }
        return result;
    }

牛人解答

public String intToRomanBM(int num) {
        String[][] c = new String[][]{
                {"","I","II","III","IV","V","VI","VII","VIII","IX"},
                {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
                {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
                {"","M","MM","MMM"}
            };
            String roman = "";
            roman += c[3][num / 1000];
            roman += c[2][num / 100 % 10];
            roman += c[1][num / 10 % 10];
            roman += c[0][num % 10];
            return roman;
  }

不得不佩服,赞一个!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容