一.题目链接: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. 在城市后面加上省,市,区 以及将MySQL入库脚本封装成class

    在城市后面加省,市,区时,使用过滤器和for循环,if判断 一起使用.   自定义一个过滤器 def my_detail(val):                                  ...

  2. jquery选择后代以及toggle,toggleClass用法

    $("#id").find(".classname")      选择id为xx下的类名为xx的元素 toggle  $(this).next(".c ...

  3. HDU2023:求平均成绩

    Problem Description 假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量. ...

  4. generator插件配置方式使用

    generator插件配置方式使用 <build> <plugins> <plugin> <groupId>org.mybatis.generator& ...

  5. python os模块使用笔记(更新)

    import os 添加os模块 walk方法: os.walk(path) path是string形式的目标目录 生成一个某目录下递归树形目录迭代器,方便递归访问子目录,访问目录就能够轻松访问子文件 ...

  6. Spring 注解bean默认名称规则

    在使用@Component.@Repository.@Service.@Controller等注解创建bean时,如果不指定bean名称,bean名称的默认规则是类名的首字母小写,如SysConfig ...

  7. SVN :Unable to connect to a repository at URL

     编程之路刚刚开始,错误难免,希望大家能够指出. 单位换地方了,SVN的服务器和本机不在一个网段,原先的SVN文件夹进行“SVN Update”的时候报错了,如下: 网上一查,原来是DNS域名解析错误 ...

  8. confluence 为合并的单元格新增一行

    1,先将最后一个结构取消合并单元格 |    | ___ | |    | ___ | | _ | ___ | 2,在最后一行追加一行,将左侧合并 3,将上面取消合并的重新合并即可

  9. Map的嵌套使用

    Map嵌套Map: 例: AAA: Javas班: 001 熊大 002 熊二 Hdoop班 001 小猪猪 002 小菲菲 ★使用增强for循环遍历Set数组: import java.util.H ...

  10. MySQL Innodb Engine --独立表空间参数(innodb_file_per_table)

    MySQL中参数innodb_file_per_table决定将表存放于ibdata*的共享表空间还是独立的.ibd文件的独立表空间. ================================ ...