【一天一道LeetCode】#12 Integer to Roman
一天一道LeetCode系列
(一)题目
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)
举例:Ⅰ、Ⅱ、Ⅲ、Ⅳ、Ⅴ、Ⅵ、Ⅶ、Ⅷ、Ⅸ、Ⅹ、Ⅺ、Ⅻ表示1-11
代码:
class Solution {
public:
string intToRoman(int num) {
string str = "";
string roman[8] = {"I","V","X","L","C","D","M"};
int j=0;
while(num)
{
int temp = num%10;
string str_t ="";
if(temp <4)
{
int count = temp;
while(count--) str_t+=roman[j];
}
else if(temp==4)
{
str_t=roman[j];
str_t+=roman[j+1];
}
else if(temp>=5&&temp<=8)
{
str_t+=roman[j+1];
int count = temp-5;
while(count--) str_t+=roman[j];
}
else if(temp==9)
{
str_t+=roman[j];
str_t+=roman[j+2];
}
cout<<roman[j]<<endl;
cout<<str_t<<endl;
str =str_t+str;
j=j+2;
num = num/10;
}
return str;
}
};
我这段代码看起来比较臃肿。看到网上的写法如beiyeqingteng博主的:
public class Solution {
public String intToRoman(int number) {
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
StringBuilder result = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (number >= values[i]) {
number -= values[i];
result.append(numerals[i]);
}
}
return result.toString();
}
}
看起来就简单多了。
【一天一道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(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- [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, ...
随机推荐
- RxJava(10-操作符原理&自定义操作符)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51791120 本文出自:[openXu的博客] 目录: 自定义创建操作符 数据序列操作符li ...
- Compass 更智能的搜索引擎(3)--高亮,排序,过滤以及各种搜索
要想使得一个搜索系统更加的完美,查询精确度和页面显示算是其中比较重要的两个方面.今天,我们就来谈谈怎么使得我们的搜索系统更加的完美. 关于分词 下载地址 配置 关于高亮 关于排序 原理 冗余字段 使用 ...
- 【安卓开发】Android系统中Parcelable和Serializable的区别
http://greenrobot.me/devpost/android-parcelable-serializable/ 进行Android开发的时候,我们都知道不能将对象的引用传给Activiti ...
- Java使用agent实现main方法之前
创建Agent项目 PreMainExecutor 类,在main方法之前执行此方法 public class PreMainExecutor { public static void premain ...
- SQLite AND/OR 运算符(http://www.w3cschool.cc/sqlite/sqlite-and-or-clauses.html)
SQLite AND/OR 运算符 SQLite 的 AND 和 OR 运算符用于编译多个条件来缩小在 SQLite 语句中所选的数据.这两个运算符被称为连接运算符. 这些运算符为同一个 SQLite ...
- SQLite 分离数据库(http://www.w3cschool.cc/sqlite/sqlite-detach-database.html)
SQLite 分离数据库 SQLite的 DETACH DTABASE 语句是用来把命名数据库从一个数据库连接分离和游离出来,连接是之前使用 ATTACH 语句附加的.如果同一个数据库文件已经被附加上 ...
- T-SQL中的APPLY用法(半翻译)
本文接上文:T-SQL 中的CROSS JOIN用法(半翻译) 同样可用于微软认证70-461: Querying Microsoft SQL Server 2012考试的学习中. --------- ...
- Android反编译 -- 错误代码还原
PS:如果阅读体验不好,可以尝试Github版 (<-点左边) 1. setColor(-16777216) 反编译的代码中会有很多setColor(int)的情况,比如setColor(-16 ...
- 【一天一道LeetCode】#342. Power of Four
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- m2eclipse Error:ArtifactTransferException: Failure to transfer org.apache.felix:org.apache.felix.res
Issue: When you create and compile a project with pom.xml using m2eclipse, it may report some error ...