Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
class Solution {
public String intToRoman(int num) {
// need to be in reverse order
StringBuilder sb = new StringBuilder();
int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
for (int i = 0; i < values.length; i++) {
// keep adding this value
while(num >= values[i]) {
sb.append(strs[i]);
num -= values[i];
}
}
return sb.toString();
} }

[LC] 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

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

  4. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 【LeetCode】12. Integer to Roman (2 solutions)

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

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

  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. 12. Integer to Roman

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

  9. 【LeetCode】12. Integer to Roman 整型数转罗马数

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

随机推荐

  1. 7.3 使用while 循环来处理列表和字典

    # 7.3.1 在列表之间移动元素 # confirmed_users.py # 首先,创建一个待验证用户列表 # 和一个用于存储已验证用户的空列表 uncomfirmed_users = ['jam ...

  2. C#用户控件的使用

    1.添加一个用户控件 2.编辑用户控件,相当于自己定义了一个控件,和其他控件一样在窗体中使用,是一个类. 右击项目,生成一下,就可以看到窗体的工具箱上面多了一组工具,可以看到我们定义的控件login ...

  3. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  4. GaussDB数据dump实现完全同步

    问题背景:搭建服务后端容灾集群,服务正常时容灾DB需要从业务DB完全同步数据,服务异常时,容灾DB停止抽取数据,自动从探针采集业务数据. 解决方案:常用的有两种思路,一是从服务后端定时每天拉取业务DB ...

  5. shell中获取文件目录方法

    1.``:表示执行对应的命令,嵌套时使用`\`\``,注意\进行转义,同时执行多个命令时使用:隔开file=`cd "\`dirname $0\`";pwd`echo $file ...

  6. JavaScript学习总结(八)

    这一节结束,我们的JavaScript学习总结系列文章第一阶段就要结束了,今后会适当的补充一些高级的内容,敬请期待. 好了,废话不说进入这一节的学习. 联动框 联动框,实在是太常见了.比如淘宝,我们选 ...

  7. 并发与高并发(八)-线程安全性-原子性-synchronized

    前言 闲暇时刻,谈一下曾经在多线程教程中接触的同步锁synchronized,相当于复习一遍吧. 主要介绍 synchronized:依赖JVM Lock:依赖特殊的CPU指令,代码实现,Reetra ...

  8. css3 flex布局详解

    原文链接: http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool https://www.cnblog ...

  9. 29. docker swarm 创建 三个节点 swarm 的集群

    1.使用 vagrant 部署 三台 centos/7 的 环境 ###Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.req ...

  10. Java 语句while、do while、for循环、嵌套、for与while的区别、break continue(3)

    for循环语句,双从for嵌套: /* for(初始化表达式:循环条件表达式:循环后的操作表达式) { 执行语句: } */ /*1,变量有自己的作用域.对于for来讲:如果将用于控制循环的增量定义在 ...