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 ...
随机推荐
- Window的匿名Closing 事件
group.Closing += (sender, e) => { try { Code here } } catch (Exception ex) { Exception here } } ...
- hdu 5423 Rikka with Tree(dfs)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- python网络请求简洁之道--python requests简介
#requests中文文档:http://cn.python-requests.org/en/latest/#学习出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0 ...
- 【百度之星2014~初赛(第二轮)解题报告】Chess
声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载.可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...
- Laravel Cheat 表 http://cheats.jesse-obrien.ca/#
Laravel Cheat Sheet Toggle Code Comments PDF Version Github Laravel 3 Docs Laravel 4 Docs Artisan ph ...
- 3DShader之移位贴图(Displacement Mapping)
我们知道法线贴图是只是改了物体的法线属性,用来计算光照,但是并没有改变物体本身的网格.但是移位贴图就不一样了,它会移动物体的顶点.我用移位贴图做了个海洋,好了,上了图再讲: 注意看海的边缘的顶点,已经 ...
- android 4.0之前版本号出现JSONException异常
今天在调试解析server传过来的JSON数据时,在2.3.7的手机上报了以下这样一个异常. 08-07 22:00:29.597: W/System.err(7610): org.json.JSON ...
- gnuplot常用技巧
一. 基础篇: 在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. 1.plot命令 gnuplot> plot sin(x) with l ...
- Hacker(25)----病毒攻防之认识病毒
Internet中,计算机病毒是威胁计算机安全的程序.对于计算机病毒,用户不仅需要掌握其基础知识,还要认识常见的病毒及简单病毒制作方法.无论病毒基础还是制作简单病毒,用户需要掌握防御病毒的有效措施和专 ...
- OWIN初探(转)
什么是 OWIN ? OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一 ...