python leetcode 387. First Unique Character in a String

利用find函数能极大地减少运行时间

class Solution:
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        c='qwertyuiopasdfghjklzxcvbnm'
        res=2**31-1
        for n in c:
            index = s.find(n)
            if index ==-1:
                continue
            if s[index+1:].find(n)==-1:
                res=min(res,index)
        return res if res!=2**31-1 else -1

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84583308