No.012:Integer to Roman
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
官方难度:
Medium
翻译:
给定一个范围在1-3999内的整数,将其转化成罗马数字。
补充资料:
罗马数字规则:
- 需要用到的罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。罗马数字中没有0。
- 一个罗马数字最多重复3次。
- 右加左减:
在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。 - 左减的数字有限制,仅限于I、X、C,且放在大数的左边只能用一个。
(*) V 和 X 左边的小数字只能用I。
(*) L 和 C 左边的小数字只能用X。
(*) D 和 M 左 边的小数字只能用C。
方法一:
- 利用一个二维数组,可以很清晰直观地根据维度来记录罗马字符。
- 获取最高位数,根据当前所在位置,对应二维数组的维度。
- 获取当前数字,4和9需要特殊处理。
- 判断当前数字是否大于等于5,将5对应的罗马字符加入结果字符串。
- 计算当前数字除以5的余数,累加1对应的罗马字符。
- 入参检查。
方法一的解题代码:
// 使用二维数组,存储罗马字符集
private static String method(int num) {
StringBuffer result = new StringBuffer();
// 罗马字符集
char[][] roman = new char[][] { { 'I', 'V' }, { 'X', 'L' }, { 'C', 'D' }, { 'M' } };
// 确定最高位数
int length = String.valueOf(num).length();
while (length-- > 0) {
// 罗马数字从左往右是最高位的
int current = (int) ((num / Math.pow(10, length)) % 10);
// 4、9特殊处理
if (current == 4) {
result.append("" + roman[length][0] + roman[length][1]);
} else if (current == 9) {
result.append("" + roman[length][0] + roman[length + 1][0]);
} else {
// 大于等于5处理
if (current / 5 == 1) {
result.append(roman[length][1]);
}
// 加1
for (int i = 0; i < current % 5; i++) {
result.append(roman[length][0]);
}
}
}
return result.toString();
}
method
方法二:
- 方法一中使用到了二维数组,可以直观地得到罗马字符,但是二维数组的存储效率是很低的。在Java中,没有二维数组的原生态概念,我们所谓的“二维数组”,其本质是数组的数组,这和C里面不同。Java中使用二维数组,无论是在存储空间还是读取速度方面,都有比较大的劣势,在有等效替代的方法情况下,尽量不要使用二维数组。
- 题中的二维数组是2*4的,对于m*n的二维数组,可以使用一维数组代替,下标索引的计算,可以参考m进制的加法原理。
方法二的解题代码:
public static String intToRoman(int num) {
if (num > 3999 || num < 1) {
throw new IllegalArgumentException("Input error");
}
StringBuffer result = new StringBuffer();
// 确定最高位数
int length = String.valueOf(num).length();
while (length-- > 0) {
// 罗马数字从左往右是最高位的
int current = (int) ((num / Math.pow(10, length)) % 10);
// 4、9特殊处理
if (current == 4) {
result.append("" + romanDict(length, 0) + romanDict(length, 1));
} else if (current == 9) {
result.append("" + romanDict(length, 0) + romanDict(length + 1, 0));
} else {
// 大于等于5处理
if (current / 5 == 1) {
result.append(romanDict(length, 1));
}
// 加1
for (int i = 0; i < current % 5; i++) {
result.append(romanDict(length, 0));
}
}
}
return result.toString();
}
// 罗马数字字典
private static char romanDict(int x, int y) {
char[] roman = new char[] { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
return roman[x * 2 + y];
}
intToRoman
相关链接:
https://leetcode.com/problems/integer-to-roman/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.012:Integer to Roman的更多相关文章
- lintcode :Integer to Roman 整数转罗马数字
题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...
- leetcode:Integer to Roman(整数转化为罗马数字)
Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- No.012 Integer to Roman
12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- LeetCode--No.012 Integer to Roman
12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- ASP.NET Core 1:UrlRouting 设置(不包含MVC6的UrlRouting设置)
0.Program.cs using System.IO; using Microsoft.AspNetCore.Hosting; namespace WebApplication1 { public ...
- ci配置smarty手记
需要用ci来写一个后台配置smarty,在网络上能够找到一些相关的文章.但是都是比较旧的内容,大部分是smary2.*的配置方法.按照这个配置后会出现一些错误.其实配置看smary官方会比较简单. 基 ...
- Text3d
有小bug,先弄这么多吧,晚了,碎觉了 ---------------------------------
- windows 程序设计自学:添加图标资源
#include <windows.h> #include "resource.h" LRESULT CALLBACK MyWndProc( HWND hwnd, // ...
- 程序员的复仇:11行代码如何让Node.js社区鸡飞狗跳
来源自:http://www.techug.com/node-js-community 几天前,一名 NPM(Node.js Package Manager)社区的贡献者 Azer Koçulu 出于 ...
- HDU 3999 The order of a Tree
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- char str[]和char *str的区别
1.http://blog.csdn.net/szchtx/article/details/10396149 char ss[]="C++"; ss[0]='c'; ...
- 如何生成ipa文件
xcode--Product--Archive 弹出Organizer-Archives选择Distribute---Save for EnterPrise or Ad_Hoc Deployment- ...
- 基于CSS3和HTML5图片加工前后对比代码
分享一款CSS3和HTML5图片加工前后对比代码.这是一款通过CSS3和HTML5将图像转换为自动响应的元素:图像缩放和裁剪以适应容器.效果图如下: 在线预览 源码下载 实现的代码. html代码 ...
- 【HTML】iframe跨域访问问题
概述 本地同一浏览器访问本地HTML文件和访问服务器端HTML文件,本地Iframe没有自适应高度,而服务器端的Ifrane自适应了高度. 1.问题重现: Chrome 版本 41.0.2272.10 ...