12. Integer to Roman (HashTable)
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。
第一步,按照千分位,百分位...一一算下来。
class Solution {
public:
string intToRoman(int num) {
int quote;
int residue;
string ret = "";
//first deal with 10^3
quote = num/;
residue = num%;
while(quote > ){
ret += 'M';
quote--;
}
//then deal with 10^2
quote = residue/;
residue %= ;
if(quote==){
ret += "CM";
}
else if(quote >=){
ret += 'D';
while(quote > ){
ret += 'C';
quote--;
}
}
else if(quote==){
ret += "CD";
}
else{
while(quote > ){
ret += 'C';
quote--;
}
}
//then deal with 10
quote = residue/;
residue %= ;
if(quote==){
ret += "XC";
}
else if(quote >=){
ret += 'L';
while(quote > ){
ret += 'X';
quote--;
}
}
else if(quote==){
ret += "XL";
}
else{
while(quote > ){
ret += 'X';
quote--;
}
}
//finally deal with 1
quote = residue;
if(quote==){
ret += "IX";
}
else if(quote >=){
ret += 'V';
while(quote > ){
ret += 'I';
quote--;
}
}
else if(quote==){
ret += "IV";
}
else{
while(quote > ){
ret += 'I';
quote--;
}
}
return ret;
}
};
第二步,代码有冗余,将其归纳整合。并且用减法代替除法!
class Solution {
public:
string intToRoman(int num) {
int values[] = {, , , , , , , , , , , , }; //数组的初始化
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int size = sizeof(values) / sizeof(values[]); //获取数组的大小
string result = "";
for (int i = ; i < size; i++) {
while (num >= values[i]) {
num -= values[i];
result+=numerals[i];
}
}
return result;
}
};
12. Integer to Roman (HashTable)的更多相关文章
- 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 ...
随机推荐
- 16款最受关注的智能手表 苹果iWatch领衔
智能手表逐渐成为科技行业的新宠,而传闻中的苹果iWatch以及已经确认即将推出的三星Galaxy Gear,显然让这股热潮达到了顶峰,也预示着大牌厂商将逐渐进入该领域,推出更多成熟的产品.以下便是16 ...
- Alpha阶段敏捷冲刺---Day1
一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 1.昨天已完成的工作 昨天我们组全体成员在五社区五号楼719召开了紧急会议,在会议上我们梳理了编写这个程序的所有流程,并且根 ...
- IIS经典模式与集成模式
在IIS7.0中Web应用程序有两种配置形式:经典和集成 经典模式 经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库,原先运行于IIS6.0下的Web应用程序迁移到IIS7 ...
- ubuntu16.04 tensorflow pip 方式安装
首先,需要知道 tensorflow 1.5版本以上包括 1.5版本 的GPU类型都是需要安装 cuda9.0的, tensorflow-gpu 1.4版本是可以使用cuda 8.0. ...
- yaf路由配置规则
使用框架的默认路由来访问的时候,会遇到一些困扰,这部分无法查看源代码,只能通过猜测来分析. 如果项目有多个模块,显然使用yaf的默认的静态路由是无法满足需求的. yaf默认的配置是着这样的: appl ...
- BZOJ3293: [Cqoi2011]分金币(数学)
3293: [Cqoi2011]分金币 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1596 Solved: 969[Submit][Status ...
- Codeforces 868F. Yet Another Minimization Problem【决策单调性优化DP】【分治】【莫队】
LINK 题目大意 给你一个序列分成k段 每一段的代价是满足\((a_i=a_j)\)的无序数对\((i,j)\)的个数 求最小的代价 思路 首先有一个暴力dp的思路是\(dp_{i,k}=min(d ...
- Ambiguous reference to member 'dataTask(with:completionHandle:)'错误
在研究IOS的网络请求过程中,因为NSURLConnection已经过时,需要引用到URLSession var url:NSURL=NSURL(string: "http://3g.163 ...
- 限制mongodb内存占用过高方法
1.mongodb必须是以服务的方式启动的.即能用service mongodb start的方式启动 资源限制用这个命令systemctl set-property <servicename& ...
- 创建自定义graphql-binding
graphql-binding 是一个比较方便强大的工具,方便我们进行代码生成以及开发gateway的功能 项目初始化 使用prisma cli 使用脚手架 prisma init appdemo ? ...