题目


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

Symol 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 is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

思路


思考罗马数字与整数的变换关系:罗马数字通常按照从大到小从左到右的顺序排,字母累加以表示数字。但是注意,整数4的罗马数字不是IIII,而是IV。像这样的数字还有9、40等。于是重新考虑建立罗马数字与整数的变换表:

Symol Value
I 1
IV 4
V 5
IX 9
X 10
XL 40
L 50
XC 90
C 100
CD 400
D 500
CM 900
M 1000

列举出所有情况下的符号,在表中查找数字num:

  1. 去Value表中找出不大于num的数字x
  2. 根据对应的Symbol输出字符,并将num-x
  3. 循环执行2操作,直到num为0

C++

class Solution {
public:
string intToRoman(int num) {
string resStr = "";
vector<int> value{1000,900,500,400,100,90,50,40,10,9,5,4,1};
vector<string> symbol{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
for(int i=0;i<value.size();i++)
while(num>=value[i])
{
resStr+=symbol[i];
num-=value[i];
}
return resStr;
}
};

Python

 def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
intList = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
strList = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
resStr = ''
i = 0
count = 0
while num > 0:
count = num/intList[i]
num %= intList[i]
while count > 0:
resStr += strList[i]
count -= 1
i += 1
return resStr

12. Integer to Roman[M]整数转罗马数字的更多相关文章

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

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  2. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  3. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  4. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  5. [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 ...

  6. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. 【LeetCode】12. Integer to Roman (2 solutions)

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  8. [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 ...

  9. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

随机推荐

  1. 实现model中的文件上传FTP(一)

    由于在django的model中配置了filefield或者imagefield配置了upload_to参数只能将用户上传的文件上传到项目本地,就算重定向到项目外也只是直接读取文件系统,这样对未来的项 ...

  2. canvas的常用api

    canvas 标签 <canvas width="600" height="400" id="canvas"></canv ...

  3. [ Java ] [ JUnit ] [ Eclipse ] coverage

    官方資訊: https://www.eclemma.org/ - 簡短使用範例說明: https://dzone.com/articles/java-code-coverage-in-eclipse ...

  4. C# 时间日期(函数,解释)

    C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6- ...

  5. swift类型操作规范

    type(of:) Applied to an object: the polymorphic (internal) type of the object, regardless of how a r ...

  6. swift pragma mark

    众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线 用#pragma mark 函数说明,来生成一个函数的说明X 但在swift中,这个语法就不支持了,毕竟它是 ...

  7. Fear No More歌词

      "Fear No More"   Every anxious thought that steals my breath It's a heavy weight upon my ...

  8. jquery里面的选择器

    jQuery 元素选择器 jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") ...

  9. Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)C. Laboratory Work

    Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some v ...

  10. nyoj286-动物统计

    动物统计 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单.科学家想判 ...