题目


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. [Codeforces]Good Bye 2017

    A - New Year and Counting Cards #pragma comment(linker, "/STACK:102400000,102400000") #inc ...

  2. (转)webpack从零开始第6课:在Vue开发中使用webpack

    vue官方已经写好一个vue-webpack模板vue_cli,原本自己写一个,发现官方写得已经够好了,自己写显得有点多余,但为了让大家熟悉webpack,决定还是一步一步从0开始写,但源文件就直接拷 ...

  3. 揭秘IPHONE X刷脸认证的技术奥秘

    苹果最新发布的Iphone X具有一个全新的功能叫做刷脸认证,背后的技术其实是生物密码的更新,通过人脸识别取代了传统的指纹识别,大家肯定对这种新技术非常感兴趣,下面我们通过这篇文章为大家介绍人脸识别的 ...

  4. apache出现You don't have permission to access / on this server提示的解决方法

    在apache的配置文件httpd.conf里定义了对网站根默认的访问权限 #<Directory />    Options FollowSymLinks    AllowOverrid ...

  5. Python:Matplotlib 画曲线和柱状图(Code)

    原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 参考资料:http://matplotlib.org/gallery.html ...

  6. 《SLIC Superpixels》阅读笔记

    原始链接:http://blog.csdn.net/jkhere/article/details/16819285 或许有改动,请参考原文! SLIC 超像素(SLICSuperpixels) Rad ...

  7. C++的Matlab接口

    与 原文 过程有些不同,根据具体环境自行配置即可! 转自于:http://blog.csdn.net/left_la/article/details/8206645 我的计算机环境是win7 64位系 ...

  8. 常用CSS代码片段常见css bug

    1.禁止文字被选中 .unselectable { -moz-user-select: -moz-none; -khtml-user-select: none; -webkit-user-select ...

  9. 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

    这是在复制代码的时候,没有修改路径,但是IDEA没有报错,还会爆出 WARN ework.web.servlet.PageNotFound - No mapping found for HTTP re ...

  10. springboot 打包下载数据

    //文件打包下载     public static HttpServletResponse downLoadFiles(List<File> files,             Htt ...