[LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral.
The number is guaranteed to be within the range from 1
to 3999
.
4
-> IV
12
-> XII
21
-> XXI
99
-> XCIX
more examples at: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
LeetCode上的原题,请参见我之前的博客Integer to Roman。
解法一:
class Solution {
public:
/**
* @param n The integer
* @return Roman representation
*/
string intToRoman(int n) {
string res = "";
vector<vector<string>> v {{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, {"M", "MM", "MMM"}};
int cnt = ;
for (int i = ; i >= ; --i) {
int t = n / cnt;
if (t) res += v[i][t - ];
n %= cnt;
cnt /= ;
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param n The integer
* @return Roman representation
*/
string intToRoman(int n) {
string res = "";
vector<int> val {, , , , , , , , , , , , };
vector<string> str{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
for (int i = ; i < val.size(); ++i) {
while (n >= val[i]) {
n -= val[i];
res += str[i];
}
}
return res;
}
};
解法三:
class Solution {
public:
/**
* @param n The integer
* @return Roman representation
*/
string intToRoman(int n) {
string res = "";
vector<string> v1 {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
vector<string> v2 {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
vector<string> v3 {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
vector<string> v4 {"", "M", "MM", "MMM"};
return v4[n / ] + v3[(n % ) / ] + v2[(n % ) / ] + v1[n % ];
}
};
[LintCode] Integer to Roman 整数转化成罗马数字的更多相关文章
- [LeetCode] 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 ...
- 012 Integer to Roman 整数转换成罗马数字
给定一个整数,将其转为罗马数字.输入保证在 1 到 3999 之间. 详见:https://leetcode.com/problems/integer-to-roman/description/ cl ...
- [Leetcode] Interger 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 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- Python如何将整数转化成二进制字符串
Python 如何将整数转化成二进制字符串 1.你可以自己写函数采用 %2 的方式来算. >>> binary = lambda n: '' if n==0 else binary( ...
- leetcode:Integer to Roman(整数转化为罗马数字)
Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...
- lintcode :Integer to Roman 整数转罗马数字
题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...
- [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 ...
随机推荐
- 用rlwrap使sqlplus可以上下翻页
下载rlwrap-0.30 从光盘上安装readline-devel和readline 安装rlwrap: #tar -zxvf rlwrap-0.30.tar.gz#cd rlwrap-0.30#. ...
- 卸载已经安装的rpm包
两个关键点: 1.如果提示有xxx.rpm包已经被installed了,那么先用rpm -e --nodeps xxx来卸载 2.如果存在多个版本的话,用rpm -e --allmatches来卸载 ...
- 管道通信,王明学learn
管道通信 一.通讯目的 1.数据传输 一个进程需要将数据发送给另一个进程. 2.资源共享 多个进程之间共享同样的资源. 3.通知事件 一个进程需要向另一个/组进程发送消息,通知它们发生了某事件. 4. ...
- IM即时通讯实现原理
即时通讯(Instant Messenger,简称IM)软件多是基于TCP/IP和UDP进行通讯的,TCP/IP和UDP都是建立在更低层的IP协议上的两种通讯传输协议.前 者是以数据流的形式,将传 ...
- Loadrunner中百分比模式和Vuser模式
从百分比模式切换到Vuser模式后,多个脚本时候,每个脚本的比例仍然维持不变: 切换到Vuser模式后: 如果在场景执行过程中需要动态添加Vuser,只能在Vuser模式下执行场景 如果需要执行“组” ...
- 利用pushState开发无刷页面切换
转载:http://www.cnblogs.com/flash3d/archive/2013/10/23/3384823.html 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与 ...
- CSS3动画属性animation的用法
转载: 赞生博客 高端订制web开发工作组 » CSS3动画属性animation的用法 CSS3提供了一个令人心动的动画属性:animation,尽管利用animation做出来的动画没有flash ...
- ember.js:使用笔记1-数组数据统一显示
ember中数据一般都是以array的形式存储的,控制器使用,如: App.DataController = Em.ArrayController.extend({}); 想要在一个页面中输出所有的数 ...
- zepto的bug2
zepto的animate()源码采用css3的方式进行,而scrollTop属性不在css3的动画属性中,所以zepto不支持animate({scrollTop:"100px" ...
- Rectangle(csu)
Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now y ...