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. Html 小插件9 腾讯新闻

    地址:http://minisite.qq.com/others08/ 效果:

  2. android 解决ViewPager双层嵌套的滑动问题

    解决ViewPager双层嵌套的滑动问题 今天我分享一下ViewPager的双层嵌套时影响内部ViewPager的触摸滑动问题 之前在做自己的一个项目的时候,遇到广告栏图片动态切换,我第一时间想到的就 ...

  3. 成都Uber优步司机快速注册攻略(外地车牌也可加入,不用现场培训)

    我加入Uber司机有一段时间了,有一些经验和感想分享给大家,让大家少走些弯路.目前加入优步不收取任何费用,不需要抢单,时间安排自由灵活,使用便捷,深受大众喜爱. 加入人民优步拼车条件:购买运行5年之内 ...

  4. zoj 3710 Friends

    #include<stdio.h> #include<string.h> ][],h; int main(int argc, char* argv[]) { int t,i,n ...

  5. MSSQL数据库统计所有表的记录数

    今天需要筛选出来库中行数不为零的表,于是动手写下了如下存储过程. CREATE PROCEDURE TableCount AS BEGIN SET NOCOUNT ON ),RowsCount INT ...

  6. mysq数据库管理工具navicat基本使用方法

    navicat是mysql数据库的客户端查询管理工具,本文详细的介绍了该软件的基本使用方法 本文转自 http://hejiawangjava.iteye.com/blog/2245758 sql是操 ...

  7. Lowest Common Ancestor of a Binary Tree, with Parent Pointer

    Given a binary tree, find the lowest common ancestor of two given nodes in tree. Each node contains ...

  8. BZOJ 1878: [SDOI2009]HH的项链( BIT )

    离线处理 , 记下询问的左右端点并排序 , 然后可以利用树状数组 , 保证查询区间时每种颜色只计算一次 ------------------------------------------------ ...

  9. libuv 错误号UV_ECANCELED 的处理

    这个地方有好多的不明白,也没仔细的看懂代码,希望有牛人指点. 先记录几个issue 1. after_write recv many(>10000) ECANCELED in my write_ ...

  10. Python 爬取CSDN博客频道

    初次接触python,写的很简单,开发工具PyCharm,python 3.4很方便 python 部分模块安装时需要其他的附属模块之类的,可以先 pip install wheel 然后可以直接下载 ...