1 题目:

Given an integer, convert it to a roman numeral.

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

Hide Tags

Math String

 
2 思路:
经过我仔细研读维基百科的说明https://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97,我发现规则好复杂,如下:
  • 重复数次:一个罗马数字重复几次,就表示这个数的几倍。
  • 右加左减:

    • 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
    • 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
    • 左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
    • 但是,左减时不可跨越一个位数。比如,99不可以用IC()表示,而是用XCIX()表示。(等同于阿拉伯数字每位数字分别表示。)
    • 左减数字必须为一位,比如8写成VIII,而非IIX。
    • 右加数字不可连续超过三位,比如14写成XIV,而非XIIII。(见下方“数码限制”一项。)
  • 加线乘千:

    • 在罗马数字的上方加上一条横线或者加上下标的Ⅿ,表示将这个数乘以1000,即是原数的1000倍。
    • 同理,如果上方有两条横线,即是原数的1000000()倍。
  • 数码限制:

    • 同一数码最多只能出现三次,如40不可表示为XXXX,而要表示为XL。
    • 例外:由于IV是古罗马神话主神朱庇特(即IVPITER,古罗马字母里没有J和U)的首字,因此有时用IIII代替IV。
想了想用if-else确定范围太难了。好吧,结果我看别人的答案,可以直接按位来确定罗马数字。(维基百科也没说可以这样转换。。)
 
3 代码:
    public String intToRoman(int num) {
String[] M = {"","M","MM","MMM"};
String[] C = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
String[] X = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String[] V = {"","I","II","III","IV","V","VI","VII","VIII","IX"}; return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + V[num%10];
}

[leetcode 12] Inter 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

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

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

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

  7. Java [leetcode 12] Integer to Roman

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

  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 (整数转罗马数字)

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

随机推荐

  1. ubuntu下安装配置ADB

    1.下载SDK Tools for Linux,地址:http://developer.android.com/sdk/index.html 2.解压,将 android-sdk-linux 文件夹放 ...

  2. Linux下使用rsync最快速删除海量文件的方法

    常用的删除命令rm -fr * 就不好用了,因为要等待的时间太长.所以必须要采取一些非常手段.我们可以使用rsync来实现快速删除大量文件. 1.先安装rsync:  yum install rsyn ...

  3. Python Numpy中transpose()函数的使用

    在Numpy对矩阵的转置中,我们可以用transpose()函数来处理. 这个函数的运行是非常反常理的,可能会令人陷入思维误区. 假设有这样那个一个三维数组(2*4*2): array ([[[ 0, ...

  4. mapreduce的输入格式 --- InputFormat

    InputFormat 接口决定了mapreduce如何切分输入文件. InputFormat 由getspilit和createRecordReader组成,getspilit主要是标记分片的初始位 ...

  5. 消息中间件及WebSphere MQ入门(转载)

    消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需要知道彼此的位置.或在继续执行前不需要等待 ...

  6. windows 10 开发学习资料,Windows-universal-samples学习笔记系列一:App settings

    windows 10 通用代码范例: https://github.com/Microsoft/Windows-universal-samples 相关视频:https://mix.office.co ...

  7. Prism-超轻量的开源框架

    http://msdn.microsoft.com/en-us/library/ff648465.aspx prism 是微软模式与实践小组开发的一个进行MVVM模式开发,其中使用依赖注入等一些方法将 ...

  8. TensorFlow安装时错误CondaValueError: prefix already exists: G:\softs\Anaconda\envs\tensorflow

    TensorFlow安装时,TensorFlow环境已经调好了,就是下面的第(3)步, 可我自己偏偏选了个Python3.7,因为检测到自己的Python最新版本为3.7,就手贱安了TensorFlo ...

  9. python之并发编程进阶篇9

    一.守护进程和守护线程 1)守护进程的概念 什么是守护进程: 守护: 在主进程代码结束情况下,就立即死掉 守护进程本质就是一个子进程,该子进程守护着主进程 为何要用守护进程 守护进程本质就是一个子进程 ...

  10. boost--线程同步

    1.互斥锁(互斥量) mutex是独占式的互斥锁.timed_mutex增加了超时功能. 成员函数:lock()用于锁定,try_lock()为非阻塞版本的锁定,unlock()用于解锁.timed_ ...