第二十周LeetCode算法题

题目名称:12. Integer to Roman

题目难度:Medium

题目描述:

Given an integer, convert it to a roman numeral.

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

题目分析:

题目没有明确给出罗马数字的语法格式,于是只能自己去百度。如下:

基本字符IVXLCDM
相应的阿拉伯数字表示为1510501005001000

1. 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;
2. 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;
3. 小的数字(限于 I、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;
4. 正常使用时、连写的数字重复不得超过三次;
5. 在一个数的上面画一条横线、表示这个数扩大 1000 倍。

所以就是写一个转换算法而已。注意点就是上面5点,写的时候要注意一下不要漏掉了某些规则即可。写的过程中真切感受到罗马数字的不方便 = = 怪不得现在在计数的时候基本被完全弃用了。
最后AC的代码是:

class Solution {
public:
    string intToRoman(int num) {
        string result;
        if (num >= 1000) {
            if (num == 1000) return "M";
            int count = num / 1000;
            for (int i = 0; i < count; ++i)
                result += "M";
            num %= 1000;
            if (num >= 900) {
                result += "CM";
                result += intToRoman(num%900);
                return result;
            } else {
                result += intToRoman(num);
                return result;
            }
        } else if (num >= 500) {
            if (num == 500) return "D";
            if (num >= 900) {
                result += "CM";
                result += intToRoman(num%900);
                return result;
            }
            int count = num / 500;
            for (int i = 0; i < count; ++i)
                result += "D";
            num %= 500;
            if (num >= 400) {
                result += "CD";
                result += intToRoman(num%400);
                return result;
            } else {
                result += intToRoman(num);
                return result;
            }
        } else if (num >= 100) {
            if (num == 100) return "C";
            if (num >= 400) {
                result += "CD";
                result += intToRoman(num%400);
                return result;
            }
            int count = num / 100;
            for (int i = 0; i < count; ++i)
                result += "C";
            num %= 100;
            if (num >= 90) {
                result += "XC";
                result += intToRoman(num%90);
                return result;
            } else {
                result += intToRoman(num);
                return result;
            }
        } else if (num >= 50) {
            if (num == 50) return "L";
            if (num >= 90) {
                result += "XC";
                result += intToRoman(num%90);
                return result;
            }
            int count = num / 50;
            for (int i = 0; i < count; ++i)
                result += "L";
            num %= 50;
            if (num >= 40) {
                result += "XL";
                result += intToRoman(num%40);
                return result;
            } else {
                result += intToRoman(num);
                return result;
            }
        } else if (num >= 10) {
            if (num == 10) return "X";
            if (num >= 40) {
                result += "XL";
                result += intToRoman(num%40);
                return result;
            }
            int count = num / 10;
            for (int i = 0; i < count; ++i)
                result += "X";
            num %= 10;
            if (num >= 9) {
                result += "IX";
                result += intToRoman(num%9);
                return result;
            } else {
                result += intToRoman(num);
                return result;
            }
        } else if (num >= 5) {
            if (num == 5) return "V";
            if (num >= 9) {
                result += "IX";
                result += intToRoman(num%9);
                return result;
            }
            int count = num / 5;
            for (int i = 0; i < count; ++i)
                result += "V";
            num %= 5;
            if (num >= 4) {
                result += "IV";
                result += intToRoman(num%4);
                return result;
            } else {
                result += intToRoman(num);
                return result;
            }
        } else {
            if (num >= 4) {
                result += "IV";
                result += intToRoman(num%4);
                return result;
            }
            int count = num / 1;
            for (int i = 0; i < count; ++i)
                result += "I";
            return result;
        }
        return result;
    }
};

虽然AC了,但是感觉自己的做法有点笨拙,于是在discuss区看了一下是不是有比较好的结果。。。以下代码令人汗颜= =||,这也太优雅了吧!

public static String intToRoman(int num) {
    String M[] = {"", "M", "MM", "MMM"};
    String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值