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. Android初学:联系创建Activity

    public class Activity2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState ...

  2. lucene 高亮显示

    原文地址: http://blog.csdn.net/javaman_chen/article/details/8224407 Lucene针对高亮显示功能提供了两种实现方式,分别是Highlight ...

  3. java_final

  4. (转)iOS Wow体验 - 第六章 - 交互模型与创新的产品概念(1)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第六章译文精选,其余章节将陆续放出.上一篇:Wow ...

  5. Linux是如何启动的

    今天早上在上操作系统课的时候,老师有提到计算机从按下开关键到最后由操作系统全然接管的整个过程. 只是讲课毕竟是十分抽象的,由于之前自己也看过这方面的内容,可是老是记不住,所以今天晚上就花了点时间,把& ...

  6. 快速构建AdapterView的Adapter--ingeniousadapter

    项目地址:ingeniousadapter 前面的话:本项目的原型是QuickAdapter,它们的思路基本一致,但本项目的优势在于: 支持AdapterView存在多个layout类型 可配置图片加 ...

  7. 0301——UItableView

    - (void)viewDidLoad { [super viewDidLoad]; self.myTableView = [[UITableView alloc]initWithFrame:CGRe ...

  8. MFC软件工程架构模型-模式窗口-非模式窗口

    1. SDI单文档界面: MDI多文档界面.有多个"关闭-最大化-最小化"等这样的窗口嵌套 基于对话框的软件模型 2.模式对话框和非模式对话框 模式对话框:使用DoMoel(),弹 ...

  9. .Net 插入数据MySql数据库,中文乱码解决问题

    1, 修改mysql根目录下配置文件my.ini,在[client]节点下添加default-character-set=utf8 ,在[mysqld]节点下添加character_set_serve ...

  10. JSONP有什么作用

    1.解决跨域访问数据                 由于同源策略的限制,XmlHttpRequest只允许请求当前源(域名.协议.端口)的资源,为了实现跨域请求,可以通过script标签实现跨域请求 ...