题目


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. vue-cli 安装

    1    node 下载      http://nodejs.cn/download/ 安装 2  npm install vue-cli -g 3  vue init <template-n ...

  2. JavaScript 消息框

    警告框 alert(); 确认框 var message=confirm("你喜欢javascript吗"); if(message==true){ document.write( ...

  3. linux VFS 之一 :虚拟文件系统的面向对象设计思想

    VFS的面向对象的思想,如下图: VFS在上层用户空间的进程与底层特定文件系统之间起到一个承上启下的作用, 对上:封装标准的系统调用接口给用户空间app,user space app不必关心特定文件系 ...

  4. C# treeView 控件

    #region --基础 ////设置目录树 ////添加根节点 //treeView1.Nodes.Add("0000000"); ////添加子节点 ////treeView1 ...

  5. Linux系统编程@进程管理(一)

    课程目标: 构建一个基于主机系统的多客户即时通信/聊天室项目 涉及的理论知识 进程控制:僵尸进程/孤儿进程.进程控制.守护进程... 进程间通信:管道.命名管道.信号... 多线程编程: 锁.信号量. ...

  6. 当使用junit4 对spring框架中controller/service/mapper各层进行测试时,需要添加的配置

    @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {&quo ...

  7. 微信小程序打开PDF

    具体情况是:微信小程序打开springboot返回的pdf文件.微信端先downloadFile,然后openDocument.但是打开文档一直不成功.后来发现官网的例子没有加fileType,我在参 ...

  8. SQL SEVER (ROLLUP与CUBE,ROW_NUMBER())使用方法

    1.建立测试专用数据: if object_id('TESTDB') is not null drop table TESTDB ), B INT) insert into TESTDB union ...

  9. node——通过express模拟Apache实现静态资源托管

    1.express中处理静态资源的函数 创建一个app.js作为入口文件,创建一个public文件夹作为静态资源文件夹 var app=express();var fn=express.static( ...

  10. `itchat`配置代理

    config.py配置 首先,先找到itchat安装的目录,然后定位到config.py文件: import os, platform VERSION = '1.3.10' BASE_URL = 'h ...