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. HDU 4622 Reincarnation(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...

  2. openStack icehouse for centos6.4 production Env 实战

    production Env brief Overview: Management Node:  controller.cc 10.114.100.115 Neutron Network Node:  ...

  3. rdo(remote data objects) repo openstack icehouse

    problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ...

  4. Javascript: Let user select an HTML element like Firebug?综述

    Javascript: Let user select an HTML element like Firebug? Javascript: Let user select an HTML elemen ...

  5. req.body取不到值的问题;

    随着express升级,bodyParser从express中被分离了出来,因此,在使用express新版本的时候,需要npm install body-parser 来安装bodyParser. 在 ...

  6. android UI进阶之用ViewPager实现欢迎引导页面[转]

    ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ViewPager主要用来组织一组数据,并且通过左右滑动的方式来展示. ...

  7. winform treeview 通过节点名称添加子节点

    /// <summary> /// 添加人员节点(利用TreeNodeCollection) /// </summary> /// <param name="t ...

  8. Javascript声明变量类型

    声明变量类型 当您声明新变量时,可以使用关键词 "new" 来声明其类型: var carname=new String; var x= new Number; var y= ne ...

  9. Your build settings specify a provisioning profile with the UUID “65a1f48d-2fa3-44eb-8142-627d9d4455b1”, however, no such provisioning profile was found.

    Your build settings specify a provisioning profile with the UUID “65a1f48d-2fa3-44eb-8142-627d9d4455 ...

  10. memset 还可以这样用

    我们经常将memset用在初始化中,其实还可以这样方便的使用它: 给数组中的一部分初始化: 看例子: #include<iostream> #include<string.h> ...