【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/

题目描述:

Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Example 1:

Input: "aba", "cdc", "eae"
Output: 3

Note:

  1. All the given strings’ lengths will not exceed 10.
  2. The length of the given list will be in the range of [2, 50].

题目大意

找出最长非公共子序列。公共子序列的定义是一个字符串可由另一个字符串删除某些字符得到。如果在一组字符串中,某一个字符串不是任何另外的字符串的公共子序列,那么这就是它是全组的非公共子序列。找出最长的长度。

解题方法

做题多了,没想到暴力解法就能过了。

写出公共子序列的判断函数。主要是两个位置pos和i,如果能找到子字符串中的某个位置,那么就把全字符串的位置后移。

寻找最长非公共子序列的时候直接两重for循环即可,不要想太复杂。

代码如下:

class Solution(object):
def findLUSlength(self, strs):
"""
:type strs: List[str]
:rtype: int
"""
longest = -1
_len = len(strs)
for i in range(_len):
issub = False
for j in range(_len):
if i == j:
continue
if self.checkSubString(strs[i], strs[j]):
issub = True
break
if not issub:
longest = max(longest, len(strs[i]))
return longest def checkSubString(self, substr, string):
if len(substr) > len(string): return False
if substr == string: return True
if not substr: return True
pos = 0
for i in range(len(string)):
if pos == len(substr):
break
if substr[pos] == string[i]:
pos += 1
return pos == len(substr)

参考资料:

  1. http://www.cnblogs.com/grandyang/p/6680548.html
  2. https://blog.csdn.net/zsensei/article/details/75227927

日期

2018 年 8 月 27 日 ———— 就要开学了!

【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)的更多相关文章

  1. LeetCode 521 Longest Uncommon Subsequence I 解题报告

    题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...

  2. 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 522. Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  4. 【leetcode】522. Longest Uncommon Subsequence II

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

  5. 522 Longest Uncommon Subsequence II 最长特殊序列 II

    详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...

  6. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  7. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  9. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. jumpserver——脚本安装

    CentOS Linux release 7.7.1908 (Core) 3.10.0-1062.4.1.el7.x86_64 Initialize(){ yum update -y systemct ...

  2. Oracle-trunc函数、round 函数、ceil函数和floor函数---处理数字函数使用

    0.round函数 按照指定小数位数进行四舍五入运算. SELECT ROUND( number, [ decimal_places ] ) FROM DUAL #number : 待处理数值  de ...

  3. 巩固javaweb第十七天

    巩固内容: 文本域 文本域主要用于输入多行文字,如果输入的文字比较多,则可以采用文本域. 文本域的基本格式如下: <textarea rows="行数" name=" ...

  4. Spark(七)【RDD的持久化Cache和CheckPoint】

    RDD的持久化 1. RDD Cache缓存 ​ RDD通过Cache或者Persist方法将前面的计算结果缓存,默认情况下会把数据以缓存在JVM的堆内存中.但是并不是这两个方法被调用时立即缓存,而是 ...

  5. MySQL压力测试工具

    一.MySQL自带的压力测试工具--Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新 ...

  6. linux之sar命令详解

    sar(System Activity Reporter系统活动情况报告)是目前Linux上最为全面的系统性能分析工具之一,可以从多个方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情况 ...

  7. zabbix之被动模式之编译安装proxy

    #:准备源码包,编译安装 root@ubuntu:/usr/local/src# ls zabbix-4.0.12.tar.gz root@ubuntu:/usr/local/src# tar xf ...

  8. eclipse.ini顺序

    -vmargs需放在-Dfile.encoding=UTF-8之前,否则会出现乱码 举例: -startup plugins/org.eclipse.equinox.launcher_1.3.0.v2 ...

  9. Jenkins配置管理工具

    安装完成后,配置管理工具 安装并配置git yum -y install git 编辑git环境变量为/usr/bin/git 安装并配置maven wget https://mirrors.tuna ...

  10. CPU状态信息us,sy等含义

    目录 一.概述概述 二.详解 us和sy ni id wa hi和si st 三.总结 一.概述概述 比如一秒内有100个cpu时间片,这个cpu时间片就是cpu工作的最小单位.那么这100个cpu时 ...