12. Integer to Roman[M]整数转罗马数字
题目
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:
- 去Value表中找出不大于num的数字x
- 根据对应的Symbol输出字符,并将num-x
- 循环执行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]整数转罗马数字的更多相关文章
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [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 ...
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
- [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 ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
随机推荐
- DropDownListFor
- ItemArray DataRow对象的RowState和DataRowVersion属性特点
DataTable.Rows[i].ItemArray DataTable.Rows表示所有行的集合DataTable.Rows[i]加上下标表示其中某一行DataTable.Rows[i].Item ...
- BZOJ3143: [Hnoi2013]游走(期望DP 高斯消元)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3597 Solved: 1618[Submit][Status][Discuss] Descript ...
- sublime text 3 上安装xdebug
安装完成之后启动xdebug,缺省设置下会显示warning等信息,很不方便. 可以参考 https://github.com/martomo/SublimeTextXdebug/blob/maste ...
- WinRAR 5.60 无广告正式版
首先明确WinRAR唯一的官网是这个 https://www.rarlab.com/ 其余的都不要相信. 现在的问题是:不要脸的中国代理强行捆绑广告:即使你花钱注册同样要面对弹窗广告!这就不可接受了! ...
- 03--软件包管理工具 apt
APT APT(the Advanced Packaging Tool)是Ubuntu 软件包管理系统的高级界面,由几个名字以“apt-”打头的程序组成.apt-get.apt-cache ...
- 01--vim常用快捷键
Linux中vim编辑器的功能非常强大,许多常用快捷键用起来非常方便,这里将我学vim入门时学的一些常用的快捷键分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉 ...
- 与PhotoZoom Classic 6相比,Classic 7新增了哪些?
PhotoZoom Classic 6凭借屡获殊荣的S-Spline XL的专利技术,以清晰和锐利的方式调整照片大小非常简单快捷甚至可以放大和改善互联网上的照片. PhotoZoom Classic ...
- 文字左右滚动选择,改变direction的值
1.从左到右滚动显示<marquee direction=left>测试</marquee> 2.从右到左滚动显示<marquee direction=right> ...
- mongoDB 介绍(特点、优点、原理)
介绍:MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 特点:高性能.易部署.易使用,存储数据非常方便. 主要功能特性有: Ø 面 ...