[LC] 12. Integer to Roman
Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(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的更多相关文章
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
- [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 ...
- [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 ...
- 12. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】12. Integer to Roman 整型数转罗马数
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
随机推荐
- 80.常用的返回QuerySet对象的方法使用详解:order_by
order_by: 将模型生成的表按照某个字段进行排序,默认情况下,按照升序的顺序排序,如果想要按照降序的顺序排序可以在字段的前面加一个"-",加一个负号就可以进行反转. mode ...
- 51nod A 魔法部落(逆元费马小定理)
A 魔法部落 小Biu所在的部落是一个魔法部落,部落中一共有n+1个人,小Biu是魔法部落中最菜的,所以他的魔力值为1,魔法部落中n个人的魔法值都不相同,第一个人的魔法值是小Biu的3倍,第二个人的魔 ...
- 第二季 第四天 part2
数据类型的转换 转化为字符串 String(value) 转型函数 这个转型函数能把任何类型的值转化为字符串 如果值有toString()方法 则用这个方法(调用没有参数的toString,默认十进制 ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- JavaSE--jdom解析之bom
参考:http://www.cnblogs.com/findumars/p/3620078.html org.jdom2.input.JDOMParseException: Error on line ...
- swoole使用异步redis
1.lnmp安装redis拓展 wget http://download.redis.io/releases/redis-4.0.9.tar.gz chmod 755 redis-4.0.9.tar. ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入数据处理框架
import tensorflow as tf # 1. 创建文件列表,通过文件列表创建输入文件队列 files = tf.train.match_filenames_once("F:\\o ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:读取MNIST手写图片数据写入的TFRecord文件
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...
- C#在listview控件中显示数据库数据
一.了解listview控件的属性 view:设置为details columns:设置列 items:设置行 1.将listview的view设置为details 2.设置列属性 点击添加,添加一列 ...
- day52-线程-队列
#1.线程的队列是使用import queue,如果使用from threading import Queue会报错,因为threading模块没有Queue. #也就是说,线程队列Queue是在qu ...