[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 ...
随机推荐
- HR外包系统 - 工资计算-几种常见账单计算规则
01-正常工资计税 (包括同一月多地计税方式) 02-年终奖计税 (包括可分批发放,但计税总额不变) 按工资 除以月份,看落在那个计税区间,获取税率和扣除数,再用总额*税率-扣除数,要考虑当月工资如 ...
- linux command file/type which/whereis
今天遇到几个命令,初见时感觉好像啊,不太能区分其具体功能和区别,因此特来记录一下. 1. file和type file: 查看文件类型 type: display information of com ...
- Vue入门笔记#数据绑定语法
#数据绑定语法# #文本: 数据绑定的基础表型形式,使用“Mustache”语法(双大括号)(特意查了一下Mustache同“moustache”释义为:髭:上唇的胡子,小胡子,最起码我觉得挺形象的) ...
- barabasilab-networkScience学习笔记4-无标度特征
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- 智能车学习(十四)——K60单片机GPIO学习
一.头文件: #ifndef __MK60_GPIO_H__ #define __MK60_GPIO_H__ #include "MK60_gpio_cfg.h" /* * 定义管 ...
- 【项目经验】之——Controller向View传值
我们的ITOO进行了一大部分了,整体上来说还是比较顺利的.昨天进行了一次验收,大体上来说,我们新生这块还是可以的.不仅仅进行了学术上的交流,还进行了需求上的更新.也正是由于这一次,我有了解到了一个新的 ...
- opacity与rgba
background: rgba(255,255,255,0.6);容器本身透明度变化,它包含的子容器的透明度不变. opacity:0.6;容器及容器包含的子容器的透明度都会发生变化.
- configSections
由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层. 这个框架的web.config文件里出现了configS ...
- 使用Soapui测试webservice接口
本来想自己写一个的,但下面这个资料比较详细(截图和步骤细节),就省事了,具体内容见: http://blog.csdn.net/oracle_microsoft/article/details/568 ...
- webApi实践:开始WebApi 2
1.学习步骤总结 学习网址:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-you ...