【leetcode】522. Longest Uncommon Subsequence II

题目如下:

解题思路:因为given list长度最多是50,我的解法就比较随意了,直接用一个嵌套的循环,判断数组中每个元素是否是其他的subsequence,最后找出不属于任何元素subsequence的最长元素即可。

代码如下:

class Solution(object):
    def isSubsequence(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: int
        """
        s = b
        for i in a:
            inx = s.find(i)
            if inx == -1:
                return False
            s = s[inx+1:]
        return True

    def findLUSlength(self, strs):
        """
        :type strs: List[str]
        :rtype: int
        """
        def cmpf(v1,v2):
            return len(v2) - len(v1)
        strs.sort(cmp = cmpf)
        if len(strs) == 0 or len(strs[0]) == 0:
            return -1
        res = -1

        visit = [0 for x in strs]
        for i in range(len(strs)):
            if visit[i] == 1:
                continue
            tmp = False
            for j in range(len((strs))):
                if i == j:
                    continue
                tmp = self.isSubsequence(strs[i],strs[j])
                if tmp == True:
                    visit[i] = 1

        for i in xrange(len(visit)):
            if visit[i] == 0:
                return len(strs[i])
        return -1

 

posted @ 2018-06-06 09:55  seyjs  阅读(223)  评论(0编辑  收藏  举报