题目:

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。

方法一:

  1. 利用一个二维数组,可以很清晰直观地根据维度来记录罗马字符。
  2. 获取最高位数,根据当前所在位置,对应二维数组的维度。
  3. 获取当前数字,4和9需要特殊处理。
  4. 判断当前数字是否大于等于5,将5对应的罗马字符加入结果字符串。
  5. 计算当前数字除以5的余数,累加1对应的罗马字符。
  6. 入参检查。

方法一的解题代码:

         // 使用二维数组,存储罗马字符集
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

方法二:

  1. 方法一中使用到了二维数组,可以直观地得到罗马字符,但是二维数组的存储效率是很低的。在Java中,没有二维数组的原生态概念,我们所谓的“二维数组”,其本质是数组的数组,这和C里面不同。Java中使用二维数组,无论是在存储空间还是读取速度方面,都有比较大的劣势,在有等效替代的方法情况下,尽量不要使用二维数组。
  2. 题中的二维数组是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/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/medium/Q012.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.012:Integer to Roman的更多相关文章

  1. lintcode :Integer to Roman 整数转罗马数字

    题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...

  2. leetcode:Integer to Roman(整数转化为罗马数字)

    Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

  3. 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 ...

  4. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  5. No.012 Integer to Roman

    12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...

  6. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  7. LeetCode--No.012 Integer to Roman

    12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...

  8. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. 【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 ...

随机推荐

  1. 【Cocos2d-Js基础教学(2)类的使用和面向对象】

    类的使用和面向对象 大家都知道在cocos2d-x 底层是C++编写的,那么就有类的概念和继承机制. 但是在JS中,是没有类这个概念的,没有提供类,没有C++的类继承机制. 那么JS是通过什么方式实现 ...

  2. Spring3系列13-Controller和@RequestMapping

    Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...

  3. Could not find the Visual SourceSafe Internet Web Service connection information for the specified database Would you like to launch the Visual sourceSafe connection wizard?

    今天同事遇到个奇怪问题,以前也遇到过,不过重新绑定一下就OK了,不知道为什么今天不行了. 错误提示:===============================================Cou ...

  4. Skynet Pomelo Erlang Elixir 的认识

    1.skynet pomelo(node.js) elixir(erlang) 周末研究总结 手游这两年发展来看,感觉对实时性要求越来越高,有同事在研究Elixir开发,google得知这东西是基于e ...

  5. 如何在wp8 中调试cocos2dx c++ 代码

    有的时候在win32上运行良好的cocos2dx程序移植到wp8的时候就出了问题,我们想把断点放到c++代码中,需要设置一下VS 2012 右击项目属性 把ui任务 设置为仅限本机 即可.

  6. 天朝专用- 配置pypi镜像

    使用python者,需要经常安装模块,可是身在天朝.pypi.python.org 站点稳定性相当差,为了很更好的使用pip安装模块. 请使用镜像. mac/linux 环境 在用户当前目录下 如没有 ...

  7. php的单例模式

    据说,单例模式是设计模式中最简单的一种. 不多说,先上代码,然后解说,一共两个文件: danli.class <?php class Danli { //保存类的实例的静态成员变量 static ...

  8. Probabilistic Graphical Models

    http://innopac.lib.tsinghua.edu.cn/search~S1*chx?/YProbabilistic+Graphical+Models&searchscope=1& ...

  9. 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreat的问题解决

    今天在写一个应用的时候,因为需要设置成横屏模式,所以在Manifest里面的Activity里面加了两个参数设置: android:screenOrientation="landscape& ...

  10. 【Head-First设计模式】C#版-学习笔记-开篇及文章目录

    原文地址:[Head-First设计模式]C#版-学习笔记-开篇及文章目录 最近一年断断续续的在看技术书,但是回想看的内容,就忘了书上讲的是什么东西了,为了记住那些看过的东西,最好的办法就是敲代码验证 ...