一.题目链接:https://leetcode.com/problems/integer-to-roman/

二.题目大意:

  给定一个整数,返回它的罗马数字的形式。

三.题解:

  要想做出这道题目,首先应该弄清楚罗马数字的规律。罗马数字中的任意一个字符连写不会重复出现4次,最多连续出现3次。题目给定的数字范围是1~3999,所以说不用特意去考虑这一点了,按照平常的思路去做就行了。给定一个罗马数字,由于它最多为4位,所以只需拆成个分位、十分位、百分位和千分位即可。对于每个位置的数字对应哪个罗马数字,只要对应起来最后拼成一起即可。代码如下:

class Solution {
public:
string intToRoman(int num) {
char* roman[4][10] = {{"","I","II","III","IV","V","VI","VII","VIII","IX"},//个分位的罗马字母
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},//百分位的罗马字母
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},//十分位的罗马字母
{"","M","MM","MMM"}};//个分位的罗马字母
string romanNum;
romanNum.append(roman[3][num / 1000 % 10]);//千分位
romanNum.append(roman[2][num / 100 % 10]);//百分位
romanNum.append(roman[1][num / 10 % 10]);//十分位
romanNum.append(roman[0][num % 10]);//个分位
return romanNum;
}
};

 本体的关键之处在于罗马数字的各个分位的数字如何表示。

LeetCode——12. Integer to Roman的更多相关文章

  1. Leetcode 12——Integer to Roman

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

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

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

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

  4. [LeetCode] 12. Integer to Roman ☆☆

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

  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. Java [leetcode 12] Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

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

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

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

  9. [leetcode] 12. Integer to Roman

    关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...

随机推荐

  1. ROC曲线和AUC值

    链接:https://www.zhihu.com/question/39840928/answer/146205830来源:知乎 一.混淆矩阵 混淆矩阵如图1分别用”0“和”1“代表负样本和正样本.F ...

  2. 【转&改进】Linux MPI 单机配置

    MPI的全称是Message Passing Interface即标准消息传递界面,可以用于并行计算.MPI有多种实现版本,如MPICH, CHIMP以及OPENMPI.这里我们采用MPICH版本. ...

  3. unity中实现简单对象池,附教程原理

    Unity对象池的创建与使用 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...

  4. HTML5的一些知识点

    1.新增很多api,比如获取用户的地理位置的window.navigator.geoloaction,history,audio,video,canvas 2.websocket;websocket是 ...

  5. xdoj 1241--余神的rp机(区间dp)

    xdoj  1241---余神的rp机 核

  6. HTML+CSS之background

    第一个专题--background属性 今天写一下background属性,具体如下: 1.background-color:默认值:transparent,这是我们在做网页时,经常使用的属性,较为简 ...

  7. 再回首 基本数据类型和 if语句

    一 变量:(使用变量是不能加引号,要不就变成字符串了) 变量的命名规则: 1.数字,字母,下划线组成. 2.变量不能是数字开头 3.区分大小写 4.不要使用中文或者拼音 5.要有相应的意义 6.不能使 ...

  8. HDU2019数列有序!

    Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序. Input 输入数据包含多个测 ...

  9. 无用之学matplotlib,numpy,pandas

    一.matplotlib学习 matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建 例子1: # coding=utf- from ...

  10. oracle 11g(64位)datebase 安装流程

    软件版本:oracle 11g 64位 datebase(二合一,含client) 系统环境:windows 10 专业版 64位操作系统 1)根据自己的操作系统去官网下载相应的安装程序,oracle ...