LeetCode第二天

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值