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 ...
随机推荐
- jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and more
jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and ...
- java 字符串为空问题
java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...
- hcharts
折线图 http://www.hcharts.cn/demo/index.php?p=10 饼状图 http://higrid.net/docs/highcharts_cn/#plotOptions- ...
- android中使用哪种方式解析XML比較好
SAX是一个用于处理XML事件驱动的"推"模型. 长处是一种解析速度快而且占用内存少的xml解析器,它须要哪些数据再载入和解析哪些内容. 缺点是它不会记录标签的关系.而要让你的应用 ...
- c++11 : range-based for loop
0. 形式 for ( declaration : expression ) statement 0.1 根据标准将会扩展成这样的形式: 1 { 2 auto&& __ra ...
- 编译U-boot时,make[1]: *** 没有规则可以创建mkimage.o”
执行完make smdk2440_config 对Uboot重行编译怎么会出现这样的错误 make[1]: Entering directory `/home/win/S3-ARM/Part4/ubo ...
- Impala 源码分析-FE
By yhluo 2015年7月29日 Impala 3 Comments Impala 源代码目录结构 SQL 解析 Impala 的 SQL 解析与执行计划生成部分是由 impala-fronte ...
- (转) .NET实现Repeater控件+AspNetPager控件分页
SqlConnection (.NET C#) 连接及分页 .net的访问数据机制决定了访问大量数据时会致使客户端机器消耗大量资源,因此有必要对数据进行分页显示,开发工具vs.net+sqlserve ...
- Struts2.3.16.3 基本9个jar包
实践证明,Struts2.3.16.3 至少要下面9个Jar包才能正常启动. commons-fileupload-1.3.1.jar commons-logging-1.1.3.jar freema ...
- 我的django之旅(三)数据库和模型
我的django之旅(三)模型和数据库 标签(空格分隔):模型 数据库 ORM 1.django ORM django内置了一套完整的解决方案,其中就包括他自己的ORM.可惜没有使用SQLAlchem ...