【LeetCode】392. Is Subsequence 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/is-subsequence/description/

题目描述:

Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).

Example 1:
s = "abc", t = "ahbgdc" Return true. Example 2:
s = "axc", t = "ahbgdc" Return false.

Follow up:

If there are lots of incoming S, say S1, S2, … , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

题目大意

有两个字符串s和t,其中s是短字符串,t是长字符串。判断s是不是t的子字符串。注意,这里应该是保留相对顺序的子字符串,也就是在s中出现的两个字符a,b应该在s中t中的相对次序相同。

解题方法

其实这个题就考了一个相对顺序。s比较短,而t很长,那么尽量就对t进行一次遍历最好。可以使用一个队列保留s的每个元素,这样对t进行遍历,如果遍历到的元素和队列的头元素相等,那么队列出头元素。这样最后返回队列是否为空即可。

class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
queue = collections.deque(s)
for c in t:
if not queue: return True
if c == queue[0]:
queue.popleft()
return not queue

如果不使用队列的话,可以使用两个指针,一个作为s的索引,一个作为t的索引。如果在t中找到了s的元素,把s的指针右移,否则把t的指针右移。

这个竟然比上面的更慢??不懂为什么。

class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
si, ti = 0, 0
while si < len(s) and ti < len(t):
if t[ti] == s[si]:
si += 1
ti += 1
return si == len(s)

方法二:

二分查找,留给二刷。

日期

2018 年 3 月 15 日 –雾霾消散,春光明媚

【LeetCode】392. Is Subsequence 解题报告(Python)的更多相关文章

  1. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  2. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  3. 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. Echart显示后端mysql数据

    一.基本思想 1.将数据存储在mysql数据库中 2.后端链接数据库,将数据库中的数据保存为json格式 3.将json格式数据使用ajax传到前端JSP页面中的Echarts 二.实现的关键点 1. ...

  2. javascript的事件循环机制

    JavaScript是一门编程语言,既然是编程语言那么就会有执行时的逻辑先后顺序,那么对于JavaScript来说这额顺序是怎样的呢? 首先我们我们需要明确一点,JavaScript是单线程语言.所谓 ...

  3. web自动化,selenium环境配置

    1,首先我们需要在python编译器中添加selenium插件,我用的是pycharm 点击下方的Terminal,然后在命令行输入: pip install selenium 也可以在设置里面手动添 ...

  4. iBatis查询时报"列名无效"或"找不到栏位名称"无列名的错误原因及解决方法

    iBatis会自动缓存每条查询语句的列名映射,对于动态查询字段或分页查询等queryForPage, queryForList,就可能产生"列名无效".rs.getObject(o ...

  5. JDBC(3):PreparedStatement对象介绍

    一,PreparedStatement介绍 PreperedStatement是Statement的子类,它的实例对象可以通过Connection.preparedStatement()方法获得,相对 ...

  6. SQL Server 和 Oracle 以及 MySQL 数据库

    推荐:https://www.zhihu.com/question/19866767 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Or ...

  7. Maven的聚合工程(多模块工程)

    在开发2个以上模块的时候,每个模块都是一个 Maven Project.比如搜索平台,学习平台,考试平台.开发的时候可以自己管自己独立编译,测试,运行.但如果想要将他们整合起来,我们就需要一个聚合工程 ...

  8. 匿名内部类与lamda表达式

    1.为什么要使用lamda表达式 从JDK1.8开始为了简化使用者进行代码开发,专门提供有Lambda表达式的支持,利用此操作形式可以实现函数式的编程,对于函数式编程比较著名的语言:haskell,S ...

  9. 【JAVA今法修真】 第一章 今法有万象 百家欲争鸣

    大家好,我是南橘,因为这段时间很忙,忙着家里的事情,忙着工作的事情,忙着考试的事情,很多时候没有那么多经历去写新的东西,同时,也是看了网上一些比较新颖的文章输出方式,自己也就在想,我是不是也可以这样写 ...

  10. 在vue3中使用router-link-active遇到的坑

    在使用 router-link-active 设置链接激活时CSS类名时,发现在例如 /member/order 和 /member/order/:id 这两个都包含 /member/order的路由 ...