problem:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

将1-3999的整数转换为罗马数字

thinking:

(1)

对比举例



个位数举例

Ⅰ,1 】Ⅱ。2】 Ⅲ,3】 Ⅳ,4 】Ⅴ。5 】Ⅵ,6】Ⅶ。7】 Ⅷ,8 】Ⅸ。9 】

十位数举例

Ⅹ。10】 Ⅺ,11 】Ⅻ,12】 XIII,13】 XIV,14】 XV,15 】XVI,16 】XVII,17 】XVIII,18】 XIX,19】 XX,20】 XXI,21 】XXII,22 】XXIX,29】 XXX,30】 XXXIV,34】 XXXV,35 】XXXIX,39】 XL,40】 L,50 】LI,51】 LV,55】 LX,60】 LXV,65】 LXXX,80】 XC,90 】XCIII,93】 XCV,95 】XCVIII,98】 XCIX,99 】

百位数举例

C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】

千位数举例

M,1000】 MC,1100 】MCD,1400 】MD,1500 】MDC,1600 】MDCLXVI,1666】 MDCCCLXXXVIII,1888 】MDCCCXCIX,1899 】MCM,1900 】MCMLXXVI,1976】 MCMLXXXIV,1984】 MCMXC,1990 】MM,2000 】MMMCMXCIX,3999】

(2)避免N多条件推断的技巧:利用罗马数字自身的规律。减去一个基数。来简化复杂度

code:

class Solution {
public:
string intToRoman(int num)
{
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result;
for (int i = 0; i < 13; i++) {
while (num >= values[i]) {
num -= values[i];
result.append(numerals[i]);
}
}
return result;
}
};

leetcode解决问题的方法||Integer to Roman问题的更多相关文章

  1. leetCode练题——12. Integer to Roman

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

  2. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

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

  3. leetcode第12题--Integer to Roman

    Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  4. LeetCode(12)Integer to Roman

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

  5. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  6. [LeetCode] Integer to Roman 整数转化成罗马数字

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

  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】Integer to Roman

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

  9. leetcode之旅(11)-Integer to Roman

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

随机推荐

  1. js匿名函数

    <script type="text/javascript"> $(function () { $("#btn").mousedown(functi ...

  2. [Mac][phpMyAdmin] [2002] No such file or directory

    我从phpMyAdmin的官网下载了最新版,将它解压到 /Library/WebServer/Documents 下,然后把文件夹改名 phpmyadmin . 接着输入在浏览器中输入 localho ...

  3. LRU与MRU算法

    1.Cache Hit and Cache Miss 当使用者第一次向数据库发出查询数据的请求的时候,数据库会先在缓冲区中查找该数据,如果要访问的数据恰好已经在缓冲区中(我们称之为Cache Hit) ...

  4. 多线程程序设计学习(8)Thread-Per-Message

    Thread-Per-Message[这个工作交给你模式]一:Thread-Per-Message的参与者--->Client(委托人)--->host(中介开线程)--->hepl ...

  5. CSS3实现jquery的特效

    实现 “慕课网” 的图片滑过缩放的效果 技术点:css3——   -webkit-transform:scale(1.2); .course-list-img .img_1:hover{ -webki ...

  6. erlang学习笔记之基础语法

    字符串是双引号,单引号的是atom元组: 下标从1开始 X = {'test1',2,3,4}. element(1,X). 配合模式匹配,可以给元素项命名,直接不用下标标记元素项 列表增删改查 增加 ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9

    (1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...

  8. Clone PDB from same CDB

    Clone PDB 用途: 1.用于测试,从生产系统clone数据来进行测试 2.诊断性能问题   Clone PDB Using OMF from same CDB 环境信息: DB Version ...

  9. Python脚本控制的WebDriver 常用操作 <二十八> 超时设置和cookie操作

    超时设置 测试用例场景 webdriver中可以设置很多的超时时间 implicit_wait.识别对象时的超时时间.过了这个时间如果对象还没找到的话就会抛出异常 Python脚本 ff = webd ...

  10. Base-Android快速开发框架(二)--数据存储之SharedPreferences

    对于App开发者,抽象来说,其实就是将数据以各种各样的方式展示在用户面前以及采集用户的数据.采集用户的数据包括用户的输入.触摸.传感器等,展示的数据通过网络来源于各业务系统,以及用户的 输入数据.在这 ...