Leetcode 13. Roman to Integer(python)

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
            

  

posted @ 2016-04-05 14:57  colors  阅读(216)  评论(0编辑  收藏  举报