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,说明当前字母的符号是加号。

 class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
my_map = {'I':1, 'V':5, 'X':10,'L':50, 'C':100, 'D':500, 'M':1000}
for i in range(len(s) - 1):
if (my_map[s[i]] - my_map[s[i + 1]]) >= 0:
result += my_map[s[i]]
else:
result -= my_map[s[i]]
result += my_map[s[len(s) - 1]]
return result

Integer to Roman

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

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

 public class Solution {
public String intToRoman(int num) {
if (num >= 1000) { return "M" + intToRoman(num - 1000); }
if (num >= 900) { return "CM" + intToRoman(num - 900); }
if (num >= 500) { return "D" + intToRoman(num - 500); }
if (num >= 400) { return "CD" + intToRoman(num - 400); }
if (num >= 100) { return "C" + intToRoman(num - 100); }
if (num >= 90) { return "XC" + intToRoman(num - 90); }
if (num >= 50) { return "L" + intToRoman(num - 50); }
if (num >= 40) { return "XL" + intToRoman(num - 40); }
if (num >= 10) { return "X" + intToRoman(num - 10); }
if (num >= 9) { return "IX" + intToRoman(num - 9); }
if (num >= 5) { return "V" + intToRoman(num - 5); }
if (num >= 4) { return "IV" + intToRoman(num - 4); }
if (num >= 1) { return "I" + intToRoman(num - 1); }
return "";
}
}

循环改为非递归

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

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. Enum Types

    参考Java的官方tutorial和Doc整理如下. What is Enum An enum type is a special data type. It enables for a variab ...

  2. 关于bootstrap--表单(按钮<button>效果、大小、禁用)

    1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...

  3. js如何获取object类型里的键值

    最近遇到一个问题: var obj = {"name1":"张三","name2":"李四"}; var key = & ...

  4. poj 3176 Cow Bowling(dp基础)

    Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...

  5. [RxJS] Starting a Stream with SwitchMap & switchMapTo

    From an event map to another event we can use switchMap(), switchMap() accept an function which retu ...

  6. hdfs经常使用命令

    hadoop hdfs经常使用命令 hadoop fs -ls /user/deploy/recsys/workspace/ouyangyewei 查看ouyangyewei文件夹文件 hadoop ...

  7. CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27

    CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27   说明:   操作系统:CentOS 6.8 32位 准备篇: 一.系统约定    软件源代码包存放位 ...

  8. CSS基础知识笔记(二)之选择器

    CSS选择器 选择器{ 样式; } 每一条css样式声明(定义)由两部分组成,形式如下: 在{}之前的部分就是“选择器”,“选择器”指明了{}中的“样式”的作用对象,也就是“样式”作用于网页中的哪些元 ...

  9. razor类型强制转换

    一.如果后台得到的是一个List类型 1.后台得到数组数据 var dutyList = IOCFactory.R<IEmployeeContract>().BatchFind(m =&g ...

  10. ArcEngine - 地图和布局同步

    1,定义同步的类: using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...