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搜索。

那么代码我就先贴出来了:

public class Solution
{
    public string IntToRoman(int num)
    {
        string result = "";
        Type R = typeof(Roman);

        foreach (var r in Enum.GetNames(R).Reverse())
        {
            while (num >= int.Parse(Enum.Format(R, Enum.Parse(R, r), "d")))
            {
                result += r.ToString();
                num -= int.Parse(Enum.Format(R, Enum.Parse(R, r), "d"));
            }
        }
        return result;
    }
}
public enum Roman
{
    M = 1000,
    CM = 900,
    D = 500,
    CD = 400,
    C = 100,
    XC = 90,
    L = 50,
    XL = 40,
    X = 10,
    IX = 9,
    V = 5,
    IV = 4,
    I = 1
};

今天晚些时候我会将 C# 枚举的一些用法贴到博客上,不了解的同学敬请关注。

除了枚举的用法外,我认为这道题中需要你去认真了解这些罗马数的规则,也就是说记得将9和4这种数也添加到枚举中哦。

那么在IntToRoman中都中都在做些什么呢?

  • 搭配Type和typeof新建出来R
  • 用foreach遍历枚举中的所有元素
  • 切记要加上Reverse(),至于为什么,大家试试不加就知道了
  • 如果num比枚举中的数字大,则将其对应的字符串(比如说”M”)添加到result中
  • 最后在num中减掉刚才已经用过的数字,数字也通过枚举来获取
  • 最后返回result

好了,到此为止,准备迎接下一题。

版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

时间: 2024-07-31 11:50:11

LeetCode 12 Integer to Roman(整型数到罗马数)的相关文章

[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 | 字符串 | 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

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

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",&