LeetCode 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

与罗马字符转数字类似,注意按级处理。

string intToRoman(int num)
{
    string sRet;
    int iNum = num;

    while(iNum > 0)
    {
        if(iNum < 5)
        {
            if(iNum == 4)
            {
                sRet += "IV";
                break;
            }
            sRet += ‘I‘;
            --iNum;
        }
        else if(5 <= iNum && iNum < 10)
        {
            if(iNum ==9)
            {
                sRet += "IX";
                break;
            }
            sRet += ‘V‘;
            iNum -= 5;

        }
        else if(10 <= iNum && iNum < 50)
        {
            if(iNum >= 40)
            {
                sRet += "XL";
                iNum -= 40;
                continue;
            }
            sRet += ‘X‘;
            iNum -= 10;
        }
        else if( 50 <= iNum && iNum < 90)
        {
            sRet += ‘L‘;
            iNum -= 50;
        }
        else if(90 <= iNum && iNum < 500)
        {
            if(iNum < 100)
            {
                sRet += "XC";
                iNum -= 90;
                continue;
            }
            if(iNum >= 400)
            {
                sRet += "CD";
                iNum -= 400;
                continue;
            }
            sRet += "C";
            iNum -= 100;
        }
        else if( 500 <= iNum && iNum < 1000)
        {
            if(iNum >= 900)
            {
                sRet += "CM";
                iNum -= 900;
                continue;
            }
            sRet += "D";
            iNum -= 500;
        }
        else if( 1000 <= iNum)
        {
            sRet += "M";
            iNum -= 1000;
        }
    }
    return sRet;
}

略长的if else语句

时间: 2024-10-25 22:46:34

LeetCode 12 integer to roman的相关文章

leetCode 12. Integer to Roman | 字符串 | Medium

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. 题目大意: 将一个给定的阿拉伯数字转换成罗马数字. 思路: 这题看到的时候,想的太多. 其实很简单,将千位,百位,十位,个位都表示出来,然后组合即可. 代码如下: class Solution { public:     string int

[LeetCode] 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. 解释: 罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500).记数的方法: 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3: 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8.

LeetCode 12 Integer to Roman(C,C++,Java,Python)

Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution: 根据数字将每一位转换为罗马字符串即可,时间复杂度O(len(num)) 题目大意: 给一个整数,将整数调整为罗马数字,关于罗马数字的定义见这里:罗马数字 个位数举例 Ⅰ,1 ]Ⅱ,2] Ⅲ,3] Ⅳ,4 ]Ⅴ,5 ]Ⅵ,6]Ⅶ,7] Ⅷ,8 ]Ⅸ

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

Q: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. A: 俺是真不知道啥是roman,我去查了一下合着是罗马字体,这题目意思也很简单就是说我要输入一个数字可以保证是1-3999但是你要把这个阿拉伯数字转换成罗马数字. 首先我们要清楚罗马数字怎么表示的,罗马数字表示如下: 罗 马 数字共有七个,即I(1),V(5),X(10),L

LeetCode 12 Integer to Roman(整型数到罗马数)

翻译 给定一个整型数值,将其转换到罗马数字. 输入被保证在1到3999之间. 原文 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 我不会告诉你一开始我是用的无数个变量和if-- 后来实在受不了这么多变量就将其写成了枚举,那么接下来就迎刃而解了. 为了让大家理解罗马数是怎么计数的,这里我截了一张图,具体的大家可以自行用微软Bing

Leetcode 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. 翻译: 给一个整数,把他转换为罗马数字输出. 这道题是简单的字符转换的问题.只要了解罗马数字代表的含义即可. 罗马数字里面只有 1,5,10.  1到3之间用重复的1表示,4用IV 90用XC 依次类推. I = 1; V = 5; X = 10; L = 50; C = 1

[LeetCode]12. Integer to Roman整数转罗马数字

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which i

LeetCode 12 Integer to Roman (整数转罗马数字)

题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", "M", "MM", "MMM”};//1000~3000String C[] = {"", "C", "CC", "CCC", "CD", "D&quo

Leetcode 12. Integer to Roman(python)

class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ M=["","M","MM","MMM"] C=["","C","CC","CCC","CD",&