13. Roman to Integer Leetcode Python

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

这题的做法和前面一体interger to roman 类似 建一个dictionary 去查询。

1.先把string 反转

2. 用一个last去保存上一次的digit

3.如果上次的digit 大于现在的digit 就将值减去两倍的digit 否则 相加。

例如iv 反转过来是VI  直接加之后等于6 实际值为4 需要减去两倍的1

代码如下

class Solution:
    # @return an integer
    def romanToInt(self, s):
        dict={'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}
        last=None
        sum=0
        s=s[::-1]
        for elem in s:
            if last and dict[last]>dict[elem]:
                sum-=2*dict[elem]
            sum+=dict[elem]
            last=elem
        return sum

引用 http://www.cnblogs.com/zuoyuan/p/3779688.html

时间: 2024-11-01 20:55:49

13. Roman to Integer Leetcode Python的相关文章

Leetcode 13. Roman to Integer(python)

class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ l=len(s) r,i=0,0 roman={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'CM':900,'CD':400,'XC':90,'XL':40,'IX':9,'IV':4} while i<l: if i&

leetCode 13. Roman to Integer 字符串

13. Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 补充:罗马数字 罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

Leetcode#13. Roman to Integer(罗马数字转整数)

题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

Leetcode 13. Roman to Integer(水)

13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve i

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

Roman to Integer [LeetCode 13]

1- 问题描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 2- 罗马数字[1] 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算. 重复数次:一个

LeetCode 13 Roman to Integer (C,C++,Java,Python)

Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 时间复杂度O(n) 题目大意: 与12题相反,给一个罗马数字,要求转化为十进制数字 解题思路: Java源代码(用时749ms): public class Solution { public int romanToInt(String s) {

LeetCode 13. Roman to Integer

https://leetcode.com/problems/roman-to-integer/#/description Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 字符串简单题,要搞清楚转换规则.如果当前字母比前一个大,则相减,比如IV = 5 - 1:否则就相加,比如VI = 5 + 1,II = 1 + 1. 罗马数字_

LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Subscribe to see which companies asked this questio 解析:给出一个罗马数字,要求把其转换为一个整数.输入范围在1到3999内. 罗马数字的规则如下: 罗马数字 I V X L C D M 代表的阿拉伯数字 1 5