Roman to Integer && Integer to Roman 解答
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 解答的更多相关文章
- 【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 ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- [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 ...
- Roman to Integer & Integer to Roman
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- list<Integer>,Integer[],int[]之间的互转(jdk1.8)
偶然在开发过程中需要将int[] 转成 List<Integer>,采用了遍历的方式,写的代码实在太多. List<Integer> list = new ArrayList& ...
- Integer to English Words 解答
Question Convert a non-negative integer to its english words representation. Given input is guarante ...
- java面试基础题------》int Integer Integer.valueOf
在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...
- PostgresException: 42883: function ifnull(integer, integer) does not exist
原因在于PostGresql并没有自带IFNULL函数,可以用COALESCE来替代IFNULL,且COALESCE功能更强大,可以输入更多参数,顺序判断并返回第一个非null值. 例如: SELEC ...
- LeetCodeOJ刷题之13【Roman to Integer】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- c语言指针与结构体之内存动态分配
struct dangdangtest { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; }; void main2() { //struct dan ...
- Java 内存区域和GC机制-java概念理解
推荐几篇关于java内存介绍的文章 Java 内存区域和GC机制 http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html ...
- qt tablewidget搜索效果如下
from PyQt4.QtCore import *from PyQt4.QtGui import *from PyQt4.Qt import *import sysfrom aa1 import U ...
- Javascript: 截取字符串多出来并用省略号[...]显示
/背景知识/ substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数描述 start 必需.一个非负的整数 ...
- Hash表题目整数hash-HDOJ1425(转载)
哈希表(散列表)的基本原理:使用一个下标范围比较大的数组来存储元素,一般通过设计一个函数(哈希函数,即散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,然后用该数组单元来存储对应 ...
- Android AdapterView View的复用机制 分析
对于ListView.GridView相信大家都不陌生,重写个BaseView,实现对于的几个方法,然后就完毕了我们的界面展示.而且在大部分情况下,我们载入特别多的Item也不会发生OOM,大家也都明 ...
- Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下
在用用 Eclipse进行Java Web开发时,web应用中引用的jar须要复制到WEB-INF下的lib目录下,否则常常出现ClassNotFound异常. 通过以下方法,能够不用手动拷贝jar包 ...
- eclipse项目转android studio详解
第一步:项目导入 向AS中导入项目的方法有两种(其实是一种). 方法一:是在eclipse中先导出为gradle(如图1),然后打开AS,找到项目中的gradle文件,直接导入. 方法二:直接在AS中 ...
- 文本输入框的两种div+css的写法
1.扁平化的设计风格.--淘宝 直接使用input.对其设置height.padding.使鼠标居中又不会占满输入框. 2.背景是图片的设计.--百度 试用span将input包裹 ...
- ADO.Net总结
ADO.NET简介 一. ADO.NET的组成(ADO.NET是什么?能干什么)客户体验 ADO.NET就是一组类库可以让我们通过程序的方式访问数据库 ADO.NET主要包括5个对象, ...