[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 ...
随机推荐
- WIN7系统下U盘安装Ubuntu双系统
1. 准备工作 1. 官网下载Ubantu镜像,我下载的是Ubantu14.04.3 LTS版本: 2. 下载 Universal USB Installer (UUI)用于制作U盘启动盘: 3. 下 ...
- apache服务器安装
下载地址:http://www.apachehaus.com/cgi-bin/download.plx 全程按这篇来的,很顺利 http://www.cnblogs.com/yerenyuan/p/5 ...
- 【Filter 页面重定向循环】写一个过滤器造成的页面重定向循环的问题
今天做一个过滤器,碰上页面重定向循环的情况: 浏览器的访问路径是:http://192.168.16.104:8080/biologyInfo/login/login/login/login/logi ...
- 【maven】 在 MyEcplise上使用maven搭建Web项目
二.在My Ecplise上使用Maven搭建Web项目 1.新建一个maven项目 2.create一个简单的骨架 3.就像在ecplise中一样设置项目的以下配置 4.新创建的项目结构如下 ...
- 笑谈Android图表-MPAndroidChart
MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活.MPA ...
- HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)
题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...
- DOM--3 DOM核心和DOM2 HTML(1)
网页是一种结构化的文档,使用一组预定义的XML和HTML标签进行标记:当浏览器接受到网页文档时,会根据文档类型和关联的样式表对其进行解析,然后以可视化形式显示在屏幕上. DOM是一组用来描述脚本怎样与 ...
- 20145223《Java程序程序设计》第4周学习总结
20145223 <Java程序设计>第4周学习总结 教材学习内容总结 面向对象中,子类继承父类避免重复的行为定义,不过并不是为了避免重复定义行为就使用继承.程序代码重复在以后修改代码的时 ...
- js与jquery异同
大家都知道jquery是js的一个库,很多东西大多数简写了,让js写起来特别的方便.但是对与学习的人来说,最好是先学会了js再去学jquery会更好.在学得过程中你会发现两者实现的原理是差不多的,但是 ...
- js中for in 和 for each in的用法和区别
区别一: for in是javascript 1.0 中发布的. for each in是作为E4X标准的一部分在javascript 1.6中发布的,而它不是EC ...