Leetcode: Roman to Integer
2015-08-26 23:25
260 查看
Question
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.
Show Tags
Show Similar Problems
Solution
Code
[code]class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ if s=="": return 0 res = 0 for ind,elem in enumerate(s): if elem=='I': if ind<len(s)-1 and (s[ind+1]=='V' or s[ind+1]=="X"): res -= 1 else: res += 1 if elem=='V': res += 5 if elem=='X': if ind<len(s)-1 and (s[ind+1]=='L' or s[ind+1]=="C"): res -= 10 else: res += 10 if elem=='L': res += 50 if elem=='C': if ind<len(s)-1 and (s[ind+1]=='D' or s[ind+1]=="M"): res -= 100 else: res += 100 if elem=='D': res += 500 if elem=='M': res += 1000 return res
相关文章推荐
- interactive/non-interactive, login/non-login shells
- 1007. Maximum Subsequence Sum (25)
- 【DirectX 2D游戏开发基础】DirectX的应用实例
- linux eth0 设置
- Linux的线程——轻进程
- MySQL性能优化的最佳20+条经验
- poj 2289 Jamie's Contact Groups 【二分 + 最大流】
- CCDrawPrimitives,CCDrawNode解析
- 移动 IOS7弹出提示
- Linux环境编程2
- Oracle exp,imp
- [转] hibernate Mysql 自增长 注解配置,表无关联的注解方式关联查询
- jst中,if的判断条件
- 同步 异步 阻塞 非阻塞机制
- 输入10个数,将其中最小的数与第一个对换,最大的数与最后一个对换
- 一个poll的简单例子
- SQL优化方法
- Httpd运维日志:通过apxs添加模块
- SEO日志:从用户搜索类型定位企业网站
- JavaScript内核笔记03-函数