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)
网友评论