13.罗马数字转整数
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
dico={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
s=list(s)
num=0
i=0
while(i<len(s)):
if(i==len(s)-1):
num+=dico[s[i]]
i+=1
elif(dico[s[i]]<dico[s[i+1]]):
num+=dico[s[i]+s[i+1]]
i+=2
else:
num+=dico[s[i]]
i+=1
return num
14.最长公共前缀
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if(strs==[]):
return ''
com=''
flag=1
len_list=[len(s) for s in strs]
min_len=min(len_list)
for i in range(min_len):
for s in strs:
if(s[i]!=strs[0][i]):
flag=0
break
if(flag==0):
break
com+=strs[0][i]
return com
20.有效的括号
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
dico={'{':1,'}':-1,'(':2,')':-2,'[':3,']':-3}
num=[dico[s[i]] for i in range(len(s))]
index=[]
for n in num:
if(n<0):
if(index==[] or index[-1]+n!=0):
return False
else:
index=index[:-1]
else:
index+=[n]
flag=True if index==[] else False
return flag