【LeetCode】392. Is Subsequence 解题报告(Python)
【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)的更多相关文章
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- Python与Perl的相似与差别
Python version 3.7版本 00.命令行交互 命令行交互 Perl Python perl -e <Perl代码> #Unix/Linux/Windows/DOS 直 ...
- ansible-playbook 编译安装nginx
mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,default,meta} -pv └── nginx ├── ...
- C#gridview尾部统计
protected void gridSettlement_RowDataBound(object sender, GridViewRowEventArgs e) { if (dtSettlement ...
- linux 实用指令时间日期类
linux 使用指令时间日期类 data 显示当前日期 基本语法 date 显示当前时间 date+%Y 显示当前年份 date+%m 显示当前月份 date+%d 显示当前是哪一天 date &qu ...
- Vue 之keep-alive的使用,实现页面缓存
什么是keep-alive 有时候我们不希望组件被重新渲染影响使用体验: 或者处于性能考虑,避免多次重复渲染降低性能.而是希望组件可以缓存下来,维持当前的状态.这时候就需要用到keep-alive组件 ...
- 【leetcode】15. 3 Sum 双指针 压缩搜索空间
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i ...
- Spring Cloud声明式调用Feign负载均衡FeignClient详解
为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...
- @RestController和@Controller的区别与作用
在springMvc中controller层类上的要使用@Controller来注明该类属于控制层,在controller层常返回的数据形式有以下几种: 页面:静态页面 ModelAndView:返回 ...
- Java Log4j 配置文件
### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.lo ...
- IOS开发入门教程-总结篇-写给狂热的编程爱好者们
程序发轻狂,代码阑珊,苹果开发安卓狂!--写给狂热的编程爱好者们 写在前面的话 学习iOS应用程序开发已有一段时间,最近稍微闲下来了,正好也想记录一下前阶段的整个学习过程.索性就从最基础的开始,一步一 ...