Middle-题目10:12. Integer to Roman

原创 2016年05月31日 15:30:16

题目原文:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.

题目大意:
把1-3999的整数转换成对应的罗马数字。
题目分析:
使用switch-case加递归,考虑所有特殊情况(遇到5,10,9等数字的时候)。
源码:(language:java)

public class Solution {
    public String intToRoman(int num) {
        if(num >= 1 && num <= 9) {
            switch (num) {
                case 1:{return "I";}
                case 2:case 3:{return "I"+intToRoman(num-1);}
                case 4:case 9:{return "I"+intToRoman(num+1);}
                case 5:{return "V";}
                default:{return "V"+intToRoman(num-5);}//6,7,8
            }
        }
        else if (num >= 10 && num <= 99) {
            switch (num) {
                case 10:{return "X";}
                case 50:{return "L";}
                case 40:case 90:{return "X"+intToRoman(num+10);}
                default: {
                    if(num > 90)
                        return "X"+intToRoman(num+10);
                    else if(num > 50)
                        return "L"+intToRoman(num-50);
                    else if(num > 40 && num < 50)  //XL
                        return "X"+intToRoman(num+10);
                    else 
                        return "X"+intToRoman(num-10);
                }
            }
        }
        else if (num >= 100 && num <= 999) {
            switch (num) {
                case 100:{return "C";}
                case 500:{return "D";}
                case 400:case 900:{return "C"+intToRoman(num+100);}
                default:{
                    if(num > 900)
                        return "C"+intToRoman(num+100);
                    else if(num > 500)
                        return "D"+intToRoman(num-500);
                    else if(num > 400 && num < 500) 
                        return "C"+intToRoman(num+100);
                    else 
                        return "C"+intToRoman(num-100);
                }
            }
        }
        else { //1000~3999
            switch (num) {
                case 1000:{return "M";}
                default:{return "M"+intToRoman(num-1000);}
            }
        }       
    }
}

成绩:
11ms,beats24.85%,众数8ms,23.08%
Cmershen的碎碎念:
代码很长也很繁琐,但感觉有一些情况是可以合并的,这样应该可以节约几ms。
此外注意,因为switch-case的每个分支都是return,故省略了break,但在其他情况下写switch一定不要忘记break。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

leetcode_middle_22_12. Integer to Roman

题意: 给定一个整数,将其变为罗马数字输出 分析: 首先来看罗马数字是怎样的: I(1),V(5),X(10),L(50),C(100),D(500),M(1000) 一个罗...

leetcode 12 -- Integer to Roman

Integer to Roman 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to...

Leetcode 12. Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 ...

LeetCode_12---Integer to Roman

LeetCode

LeetCode 12_Integer to Roman

这几天没别的事情,抓紧时间多写几道。今天给大家带来的是leetcode第12题,题目要求也很简单直接,就是把阿拉伯数字。其实呢,阿拉伯数字是印度人发明的,阿拉伯人只是二道贩子,有兴趣的朋友可以去查一下...

[leetcode] 12. Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 ...

Leetcode Q12 : Integer to Roman

题目12: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

[LeetCode][12]Integer to Roman解析 int转罗马字符时间复杂度为常数的实现-Java实现

Q: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the ran...

LeetCode12. Integer to Roman不用任何数据结构和数组

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

LeetCode之12---Integer to Roman

题目:   Given an integer, convert it to a roman numeral.   Input is guaranteed to be within the rang...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)