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
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can 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 bit(int i, int id){
string s;
if(id == ){
if(i==) s = "M";
else if(i==) s = "MM";
else if(i==) s = "MMM";
}
else if(id==){
if(i==) s = "C";
else if(i==) s = "CC";
else if(i==) s = "CCC";
else if(i==) s = "CD";
else if(i==) s = "D";
else if(i==) s = "DC";
else if(i==) s = "DCC";
else if(i==) s = "DCCC";
else if(i==) s = "CM";
else s = "";
}
else if(id==){
if(i==) s = "X";
else if(i==) s = "XX";
else if(i==) s = "XXX";
else if(i==) s = "XL";
else if(i==) s = "L";
else if(i==) s = "LX";
else if(i==) s = "LXX";
else if(i==) s = "LXXX";
else if(i==) s = "XC";
else s = "";
}
else if(id==){
if(i==) s = "I";
else if(i==) s = "II";
else if(i==) s = "III";
else if(i==) s = "IV";
else if(i==) s = "V";
else if(i==) s = "VI";
else if(i==) s = "VII";
else if(i==) s = "VIII";
else if(i==) s = "IX";
else s = "";
}
return s;
}
string intToRoman(int num) {
string ans;
ans += bit(num/,);
num %=;
ans += bit(num/,);
num %=;
ans += bit(num/,);
num %=;
ans += bit(num,);
return ans;
} };
Leetcode 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 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [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 ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [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 ...
- LeetCode——12. Integer to Roman
一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- [leetcode] 12. Integer to Roman
关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...
随机推荐
- 【MM系列】SAP MM模块-打开前面物料账期的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]在SAP里查看数据的方法 前言部 ...
- Lesson 4 The double life of Alfred Bloggs
There are two type of people in the society. People who do manual works can higher payment than peop ...
- 学了一天的golang从入门到放弃
Google的go就是个二货,不实用,它最多只能和c比简单.low!
- 【Qt开发】Qt Creator在Windows上的调试器安装与配置
Qt Creator在Windows上的调试器安装与配置 如果安装Qt时使用的是Visual Studio的预编译版,那么很有可能就会缺少调试器(Debugger),而使用MSVC的Qt对应的原生调试 ...
- 关于migration build failed的问题
首先一定要执行dotnet restore 查看网站的依赖关系(有时候生成是不报错的但是restore会找不到文件路径) 检查执行命令的路径是否是正确的当前网站路径 build failed一定是生成 ...
- MySQL数据库的特点和优势
MySQL数据库的特点和优势: 1.MySQL性能卓越.服务稳定,很少出现异常宕机. 2.MySQL开放源代码且无版权制约,自主性及使用成本低. 3.MySQL历史悠久,用户使用活跃,遇到问题可以寻求 ...
- babel版本问题
在运行webpack命令的时候总是报错,原来是因为babel版本的问题 我安装的webpack3 babel版本是6 babel-loader是8 后来把babel改成7就可以了
- vue视图不更新情况详解
vue视图不更新情况详解:https://www.jb51.net/article/161371.htm
- 为什么你的javascript学了这么久,水平还是烂成了渣?
今年我给公司面试时,面试了百来个人,水平我就呵呵了,还觉得自己学了很久很厉害了,其实呢,渣的很呀,这篇文章送给想学好javascript找份工作的同学们. 首先要说明的是,咱现在不是高手,最多还是一个 ...
- 消息中间件 JMS入门
1. JMS入门 1.1消息中间件 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环 ...