美文网首页
521. Longest Uncommon Subsequenc

521. Longest Uncommon Subsequenc

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-08-13 10:37 被阅读0次

    521. Longest Uncommon Subsequence I

    521. Longest Uncommon Subsequence I

    这是什么题,excuse me ?

    class Solution(object):
        def findLUSlength(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: int
            """
            if a == b:
                return -1
            else:
                return max(len(a),len(b))
    

    522. Longest Uncommon Subsequence II

    522. Longest Uncommon Subsequence II

    刚开始没有看懂题,Subsequence是可以删掉中间的字符,所以要用两个指针,看看是不是能指向最后,优化就是把输入按照长度进行了一个排序,这样找到的第一个就是结果。

    class Solution(object):
        def findLUSlength(self, strs):
            """
            :type strs: List[str]
            :rtype: int
            """
            strs.sort(key=lambda x:len(x), reverse=True)
            for i in range(len(strs)):
                count = len(strs) - 1
                for j in range(len(strs)):
                    if i == j: continue
                    if self.help(strs[i], strs[j])==False:
                        count -= 1
                if count == 0:
                    return len(strs[I])
            return -1
        
        def help(self, s1, s2):
            if len(s1) > len(s2):
                return False
            if s1 == s2:
                return True
            i = j = 0
            while i < len(s1) and j < len(s2):
                if s1[i] == s2[j]:
                    i += 1
                    j += 1
                else:
                    j += 1
            return i == len(s1)
    

    相关文章

      网友评论

          本文标题:521. Longest Uncommon Subsequenc

          本文链接:https://www.haomeiwen.com/subject/diamjctx.html