首页 > 编程语言 > 详细

Leetcode 13. Roman to Integer(python)

时间:2016-04-05 15:50:05      阅读:221      评论:0      收藏:0      [点我收藏+]
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<l-1 and roman.has_key(s[i:i+2]):
    			r+=roman[s[i:i+2]]
    			i+=2
    		else:
    			r+=roman[s[i]]
    			i+=1
    	return r
            

  

Leetcode 13. Roman to Integer(python)

原文:http://www.cnblogs.com/colorss/p/5355112.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!