【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. day03 MySQL数据库之主键与外键

    day03 MySQL数据库之主键与外键 昨日内容回顾 针对库的基本SQL语句 # 增 create database meng; # 查 show databases; shwo create da ...

  2. Scala(八)【面向对象总结】

    面向对象总结 面向对象 1.scala包 1.声明包 1.在文件第一行通过package 包名 2.package 包名{ .... } 第二种方法,包名只能在target目录才能看到 2.导入包 1 ...

  3. 案例 stm32的dma传输过程

    首先说一下:DMA_GetCurrDataCounter返回值是什么 返回值是dma缓存里还剩余多少空间. 上面本来应该是,发一下,改变一下.但是这里有一行是特殊的. long : 461,*ff l ...

  4. Oracle中的job(定时任务)

    oracle中的job(定时任务)由dbms_job包下的函数实现.关于job的理论知识可参考https://blog.csdn.net/apextrace/article/details/77675 ...

  5. HongYun-ui搭建记录

    vue项目windows环境初始化 Element-ui使用 vue2 页面路由 vue SCSS 在VUE项目中使用SCSS ,对SCSS的理解和使用(简单明了) vue axios vue coo ...

  6. 在应用程序中的所有其他bean被销毁之前执行一步工作

    1.实现ServletContextListener.ApplicationContextAware两个接口,在销毁方法里借助ApplicationContextAware注入的application ...

  7. Initialization of data members

    In C++, class variables are initialized in the same order as they appear in the class declaration. C ...

  8. Win10 Chrome 在DPI缩放下导致界面放大问题 解决方案

    支持:54.0.2840.59 m (64-bit) 以下大多数版本,具体未测试.如有问题可以反馈一下. 方法1:为程序设置"高DPI设置时禁用显示缩放. 方法2:为程序添加启动参数: /h ...

  9. 1.RabbitMQ

    1.RabbitMq是什么?    MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队 ...

  10. Spring中Bean的装配方式

    一.基于xml的装配 Student.java package com.yh; public class Student implements People { public void breath( ...