Given a roman numeral, convert it to an integer.

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

相比Interger to Roman,本题明显要简单一些,按照规则直接翻译就好。

AC代码:

class Solution(object):
def romanToInt(self, s):
roman_to_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
i, num = 0, 0
while i < len(s) - 1:
if roman_to_int[s[i]] >= roman_to_int[s[i + 1]]:
num += roman_to_int[s[i]]
else:
num -= roman_to_int[s[i]]
i += 1
num += roman_to_int[s[i]]
return num

13. Roman to Integer的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  4. C# 写 LeetCode easy #13 Roman to Integer

    13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and  ...

  5. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  6. 【LeetCode】13. Roman to Integer (2 solutions)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  7. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 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 ...

  9. 13. Roman to Integer[E]罗马数字转整数

    题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  10. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

随机推荐

  1. linux Bash bug修复记录

  2. JAVA FILE or I/O学习 - 补充CopyFiles功能

    public class CopyFiles { public static void main(String[] args) { CopyFiles copyFiles = new CopyFile ...

  3. aliyun opts 集锦

    <一,>,aliyun 使用数据盘(aliyun新增数据盘使用,创建vg,aliyun 镜像系统本身未使用lvm-vg-lv) 1.1直接挂载文件系统 较易,不做分析 http://hel ...

  4. 视频主观质量评价工具:MSU Perceptual Video Quality tool

    MSU Perceptual Video Quality tool是莫斯科国立大学(Moscow State University)的Graphics and Media Lab制作的一款视频主观评价 ...

  5. SQL Server2012新特性概述

    公司最近要升级数据库,SQL Server 2008R2-->2012.再开始升级之前先找了点资料分析一下2012的新特性和功能,提前预热一下. 2012中主要关注一下三个领域: 性能:改进的核 ...

  6. Objective-c (多输入参数的方法)

    一个方法可能具有多个输入参数.在头文件中,可以定义带有多个输入参数的方法: - (void)setIntX:(int)n andSetIntY:(int)d 下面通过一个例子来说明它的具体用法: #i ...

  7. Set 与 Multiset

    Set 与 Multiset 会根据待定的排序准则,自动将元素排序,两者不同之处在于前者不允许元素重复,后者允许,下面介绍一下set中的函数: 一.set 中的 begin.end.rbegin.re ...

  8. Maven POM配置释疑

    1.  对于有父子关系的Project, 父POM中依赖使用dependencies 和 dependencyManagement 的区别: dependencies: 即使子项目中不写该依赖项,仍然 ...

  9. IOS 单例模式的学习

    单例模式只能修改无法释放,直到程序结束. 我们下面一步一步来做一个单例模式程序 (1)单例一旦创建,是永远存在于内存中的,所以需要创建一个全局量 static MySingletonClass *sh ...

  10. First AngularJS !

    My first angular! <html ng-app> <head> <meta charset="utf-8"> <script ...