题目


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. meta标签的作用及整理

    [转载] meta的标签的使用是我在前端学习中曾经困惑过一段时间的问题.一方面不是很了解meta标签的用途,另一方面是对于meta标签里的属性和值不是懂,也不知道从哪里冒出来的,所以这篇文章专门整理下 ...

  2. webpack打包大概流程

    webpack 步骤 1. 新建一个webpack.config.prod.js 2. 压缩bundle.js和index.html //设置为生产环境 new webpack.DefinePlugi ...

  3. javascript中对象属性的介绍

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. How to share memory between services and user processes?

    除了必要的InitializeSecurityDescriptor和SetSecurityDescriptorDacl, 内存映射文件名必须GLOBAL开头.

  5. Oracle PL/SQL开发基础(第三十四弹:RAISE_APPLICATION_ERROR)

    RAISE_APPLICATION_ERROR在子程序内部使用时,能从存储子程序中抛出自定义的错误消息.这样就能将错误报告给应用程序而避免范围未捕获异常. 语法如下: RAISE_APPLICATIO ...

  6. 快速搭建一个本地的FTP服务器 win10及win7

    快速搭建一个本地的FTP服务器   如果需要开发FTP文件上传下载功能,那么需要在本机上搭建一个本地FTP服务器,方便调试. (win10) 第一步:配置IIS Web服务器 1.1 控制面板中找到“ ...

  7. BZOJ 1725: [Usaco2006 Nov]Corn Fields牧场的安排 状压动归

    Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧 ...

  8. node——express实现hello world

    创建文件夹,在文件夹内再创建index.js 1.package.json npm init -y 2.安装 npm install express ---save 3.index.js //入口文件 ...

  9. nodejs 使用crypto实现sha256\md5加密

    var crypto = require('crypto'); var hash = crypto.createHash('sha256');// sha256或者md5 hash.update('1 ...

  10. npm run build 打包项目,图片等资源使用相对路径会出现路径错误的问题

    在build下的utils.js中,3使用 ‘vue-style-loader’ 依赖的地方添加 publicPath: '../../' , 如图: