[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 ...
随机推荐
- hook键盘钩子_非dll
unit Unit1; // download by http://www.codefans.net interface uses Windows, Messages, SysUtils, Class ...
- 2019.3.12 linux关于用户的一些命令
su:默认切换到root 创建用户 adduser :新建一个用户 sudo adduser 新用户名字 :创建新用户 sudo passwd 用户名:修改该用户名的密码 创建组 sudo addgr ...
- SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:6-3 CASE表达式
● 虽然CASE表达式中的ELSE子句可以省略,但为了让SQL语句更加容易理解,还是希望大家不要省略. ● CASE表达式中的END不能省略. ● 使用CASE表达式能够将SELECT语句的结果进行组 ...
- jstl中遍历Map
在jstl中遍历Map和遍历List与数组一样,都是使用forEach标签. 例子: <%@ page import="java.util.Map" %> <%@ ...
- UML-架构分析-阶段
初始阶段:架构概念验证原型--->确定其可行性 细化阶段:因素表.技术备忘录.SAD(软件架构文档) 移交阶段:可能会修改SAD->确保与最终部署版本的一致性 后续进化循环:重温架构性因素 ...
- 解决 springweb Filter 读取request body miss body
package com.lb.demo.listener; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- Linux--Shell传递参数
参考:http://www.runoob.com/linux/linux-shell-passing-arguments.html
- frp内网穿透,centos7+frp成功样例
准备工作: 阿里云服务器一台,备案域名一个,本地服务器一台(本人用的虚拟机centos7) frp文件:frp_0.22.0_linux_amd64.tar.gz 链接:https://pan.bai ...
- 洛谷 P1341 无序字母对(欧拉回路)
题目传送门 解题思路: 一道欧拉回路的模板题,详细定理见大佬博客,任意门 AC代码: #include<cstdio> #include<iostream> using nam ...
- Bug(1)
程序要求:内网之间客户端截屏(.bmp)并传送给服务端. server: #include <winsock2.h> // 为了使用Winsock API函数 #include <s ...