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 ...
随机推荐
- 通过PowerShell发送TCP请求
很多时候我们需要通过Socket发送特定的TCP请求给服务器的特定端口来实现探测服务器的指定端口所开启的服务.很多语言都有相应的方法实现上述需求,当然,PowerShell也不例外,比如我们要发送一个 ...
- 网络存储-Samba、NAS---未知的用户名或错误密码
项目中的文件需要保存到网络存储设备中,之前用的是NAS.因没来得及采购就先用Samba顶上.代码发现通用…… 一.定义: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器 ...
- haproxy 配置
1.环境: 操作系统:CentOS 6.4 haproxy: 1.3.15.10 [下载:http://download.chinaunix.net/download.php?id=25784& ...
- 关于app.config不能即时保存读取的解决方案
public void saveValue(string Name, string Value) { ConfigurationManager.AppSettings.Set(Name, Value) ...
- js深拷贝和浅拷贝
一.数组的深浅拷贝 在使用JavaScript对数组进行操作的时候,我们经常需要将数组进行备份,事实证明如果只是简单的将它赋予其他变量,那么我们只要更改其中的任何一个,然后其他的也会跟着改变,这就导致 ...
- sqlmap查找SQL注入漏洞入门
1.安装sqlmap sqlmap是一款非常强大的开源sql自动化注入工具,可以用来检测和利用sql注入漏洞.注意:sqlmap只是用来检测和利用sql注入点的,使用前请先使用扫描工具扫出sql注入点 ...
- 我的第一个Socket程序-SuperSocket使用入门(三)
本来博客都停了,不打算更了,但今天百度一个socket的问题时无意间发现第一篇的socket文章权重仅次于SuperSocket网站,顿时觉得自己6到不行,再写一篇,讨论下数据持久化的问题 去年搞那个 ...
- 【原】Configuring Oracle Data Guard In Physical Standby Database
作者:david_zhang@sh [转载时请以超链接形式标明文章] http://www.cnblogs.com/david-zhang-index/p/5042640.html参照文档:https ...
- ListView具有多种item布局——实现微信对话列
这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信.whatsapp.易信.米聊等.我们这篇文章也权当为回忆,形成简单的笔记.这篇文章参考了2009年Google IO中的< ...
- [转载]QString 乱谈(3)-Qt5与中文
原文地址http://blog.csdn.net/dbzhang800/article/details/7542672?reload 两个月前,简单写过QTextCodec中的setCodecForT ...