Roman Numeral Chart

V:5  X:10  L:50  C:100  D:500  M:1000

规则:

1. 重复次数表示该数的倍数
2. 右加左减:
较大的罗马数字右边记上较小的罗马数字,表示大数字加小数字
较小的罗马数字右边记上较大的罗马数字,表示大数字减小数字
左减的数字有限制,仅限于I, X, C
左减时不可跨越一个位数。如,99不可以用IC(100 - 1)表示,而是XCIX(100 - 10 + 10 - 1)
左减数字必需为一位
右加数字不可连续超过三位

Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

基本思路是每次比较当前字母和它下一个字母,如果是increasing order,说明当前字母的符号是减号,如果是decreasing order,说明当前字母的符号是加号。

  1. class Solution(object):
  2. def romanToInt(self, s):
  3. """
  4. :type s: str
  5. :rtype: int
  6. """
  7. result = 0
  8. my_map = {'I':1, 'V':5, 'X':10,'L':50, 'C':100, 'D':500, 'M':1000}
  9. for i in range(len(s) - 1):
  10. if (my_map[s[i]] - my_map[s[i + 1]]) >= 0:
  11. result += my_map[s[i]]
  12. else:
  13. result -= my_map[s[i]]
  14. result += my_map[s[len(s) - 1]]
  15. return result

Integer to Roman

根据规则,可以用递归和非递归的方法解答。

注意到规则,跨越的位数不可超过一位。

  1. public class Solution {
  2. public String intToRoman(int num) {
  3. if (num >= 1000) { return "M" + intToRoman(num - 1000); }
  4. if (num >= 900) { return "CM" + intToRoman(num - 900); }
  5. if (num >= 500) { return "D" + intToRoman(num - 500); }
  6. if (num >= 400) { return "CD" + intToRoman(num - 400); }
  7. if (num >= 100) { return "C" + intToRoman(num - 100); }
  8. if (num >= 90) { return "XC" + intToRoman(num - 90); }
  9. if (num >= 50) { return "L" + intToRoman(num - 50); }
  10. if (num >= 40) { return "XL" + intToRoman(num - 40); }
  11. if (num >= 10) { return "X" + intToRoman(num - 10); }
  12. if (num >= 9) { return "IX" + intToRoman(num - 9); }
  13. if (num >= 5) { return "V" + intToRoman(num - 5); }
  14. if (num >= 4) { return "IV" + intToRoman(num - 4); }
  15. if (num >= 1) { return "I" + intToRoman(num - 1); }
  16. return "";
  17. }
  18. }

循环改为非递归

  1. public class Solution {
  2. public String intToRoman(int num) {
  3. String result = "";
  4. String [] symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  5. int [] value = {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  6. for(int i=0;num!=0;i++){
  7. while(num >= value[i]){
  8. num -= value[i];
  9. result += symbol[i];
  10. }
  11. }
  12. return result;
  13. }
  14. }

Roman to Integer && Integer to Roman 解答的更多相关文章

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

  2. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  3. [string]Roman to Integer,Integer to Roman

    一.Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within ...

  4. Roman to Integer & Integer to Roman

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  5. list<Integer>,Integer[],int[]之间的互转(jdk1.8)

    偶然在开发过程中需要将int[] 转成 List<Integer>,采用了遍历的方式,写的代码实在太多. List<Integer> list = new ArrayList& ...

  6. Integer to English Words 解答

    Question Convert a non-negative integer to its english words representation. Given input is guarante ...

  7. java面试基础题------》int Integer Integer.valueOf

    在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...

  8. PostgresException: 42883: function ifnull(integer, integer) does not exist

    原因在于PostGresql并没有自带IFNULL函数,可以用COALESCE来替代IFNULL,且COALESCE功能更强大,可以输入更多参数,顺序判断并返回第一个非null值. 例如: SELEC ...

  9. LeetCodeOJ刷题之13【Roman to Integer】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

随机推荐

  1. Window的匿名Closing 事件

    group.Closing += (sender, e) => { try {   Code here } } catch (Exception ex) { Exception here } } ...

  2. hdu 5423 Rikka with Tree(dfs)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  3. python网络请求简洁之道--python requests简介

    #requests中文文档:http://cn.python-requests.org/en/latest/#学习出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0 ...

  4. 【百度之星2014~初赛(第二轮)解题报告】Chess

    声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载.可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...

  5. Laravel Cheat 表 http://cheats.jesse-obrien.ca/#

    Laravel Cheat Sheet Toggle Code Comments PDF Version Github Laravel 3 Docs Laravel 4 Docs Artisan ph ...

  6. 3DShader之移位贴图(Displacement Mapping)

    我们知道法线贴图是只是改了物体的法线属性,用来计算光照,但是并没有改变物体本身的网格.但是移位贴图就不一样了,它会移动物体的顶点.我用移位贴图做了个海洋,好了,上了图再讲: 注意看海的边缘的顶点,已经 ...

  7. android 4.0之前版本号出现JSONException异常

    今天在调试解析server传过来的JSON数据时,在2.3.7的手机上报了以下这样一个异常. 08-07 22:00:29.597: W/System.err(7610): org.json.JSON ...

  8. gnuplot常用技巧

      一.         基础篇: 在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. 1.plot命令 gnuplot> plot sin(x) with l ...

  9. Hacker(25)----病毒攻防之认识病毒

    Internet中,计算机病毒是威胁计算机安全的程序.对于计算机病毒,用户不仅需要掌握其基础知识,还要认识常见的病毒及简单病毒制作方法.无论病毒基础还是制作简单病毒,用户需要掌握防御病毒的有效措施和专 ...

  10. OWIN初探(转)

    什么是 OWIN ? OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一 ...