We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a27d247 commit d03fa49Copy full SHA for d03fa49
solution/013.Roman to Integer/Solution2.py
@@ -0,0 +1,10 @@
1
+class Solution(object):
2
+ def romanToInt(self, s):
3
+ dict = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1} #对应关系存在一个字典里
4
+ sum = 0
5
+ for i in range(len(s)-1):
6
+ if dict[s[i]] < dict[s[i+1]]: #这里小于是因为只会有一个减的存在,换句话说如果两个一样的出现,应该是加法
7
+ sum -= dict[s[i]]
8
+ else:
9
+ sum += dict[s[i]]
10
+ return sum+dict[s[-1]] #最后一位一定是加
0 commit comments