leetcode解决问题的方法||Integer to Roman问题

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;
    }
};
时间: 2024-08-21 19:25:16

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

LeetCode(12)Integer to Roman

题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 分析 该题目要求将给定的1~3999之间的整型数字转换为罗马数字并输出. 解这道题我们必须了解罗马字母与整数之间的对应: 对照举例如下: AC代码 class Solution { public: string intToRoman(int num) { //存储罗马数字 st

leetcode第12题--Integer to Roman

Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 把阿拉伯数字转换为罗马数字输出.百度一下对应的 I V X L C D M,代表1,5,10,50,100,500,1000 然后写一个子函数,输入数字和相应的位数级别,如个位为level 1,千为4.因为最多不会超过四千.所以可以如下.注意了,string用法很好

[LeetCode][Python]Integer to Roman

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/integer-to-roman/Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.===Comments by Daba

LeetCode:Integer to Roman

1.题目名称 Integer to Roman (阿拉伯数字到罗马数字的转换) 2.题目地址 https://leetcode.com/problems/integer-to-roman 3.题目内容 英文:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 中文:给出一个整数,将它转换成罗马数字.输入在1-3999之间. 4.题目分

LeetCode 012 Integer to Roman

[题目] Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [题意] 给定一个整数,将其表示成罗马数字 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 罗马数组数规则: 基本数字Ⅰ.X .C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上

【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

Integer to Roman leetcode java

题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解: 这道题..还有哪个roman to integer..第一件事 就是先把roman认全吧.. 罗马数字拼写规则(转自Wikipedia:http://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97)

【leetcode刷题笔记】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解:基本的罗马字符和数字对应如下表所示: 罗马字符 数字 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表: 罗马字符 数字 IV 4 IX