[Leetcode 392]判断子序列 Is Subsequence
【思路】
判断s是否为t的子串,所以length(s)<=length(t)。于是两个指针,一次循环。
将s、t转换为数组p1、p2。
i为过程中s的匹配长度。
i=0空串,单独讨论返回true。
当i=p1.length-1时返回true,否则循环结束返回false。
【代码】
class Solution {
public boolean isSubsequence(String s, String t) {
char []p1=s.toCharArray();
char []p2=t.toCharArray();
if(p1.length==0){
return true;
}
int i=0;
for(int j=0;j<p2.length;j++){
if(p2[j]==p1[i]){
if(i==p1.length-1){
return true;
}
i++;
}
}
return false;
}
}
【举例】
[Leetcode 392]判断子序列 Is Subsequence的更多相关文章
- Java实现 LeetCode 392 判断子序列
392. 判断子序列 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符 ...
- Leetcode 392.判断子序列
判断子序列 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 ...
- [LeetCode] 392. 判断子序列 ☆(动态规划)
https://leetcode-cn.com/problems/is-subsequence/solution/java-dp-by-zxy0917-5/ 描述 给定字符串 s 和 t ,判断 s ...
- Leetcode之动态规划(DP)专题-392. 判断子序列(Is Subsequence)
Leetcode之动态规划(DP)专题-392. 判断子序列(Is Subsequence) 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母. ...
- [Swift]LeetCode392. 判断子序列 | Is Subsequence
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- LeetCode 792. 匹配子序列的单词数(Number of Matching Subsequences)
792. 匹配子序列的单词数 792. Number of Matching Subsequences 相似题目 392. 判断子序列
- [leetcode] 392. Is Subsequence (Medium)
原题 判断子序列 /** * @param {string} s * @param {string} t * @return {boolean} */ var isSubsequence = func ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- centos 安装 FLEXPART
师哥做了个课题,用FLEXPART分析大气伴飞轨迹,提前先安装这个软件吧.我使用的环境是centos7,看官慢慢看,结尾有彩蛋~ 准备工作,flexpart是用Fortran语言写的,以.90结尾的文 ...
- spring boot: 支持jsp,支持freemarker
spring boot: 支持jsp,支持freemarker 支持jsp: 加入依赖 <!--jsp--> <dependency> <groupId>org.a ...
- Linux(centos7)上安装最新版R3.4.1
说来惭愧,居然没有在Linux安装R的经验,因为一直很少用R,用也是在win平台. 下载路径:https://cran.rstudio.com/src/base/R-3/ 强烈建议不要安装最新的R,除 ...
- LeetCode--005--最长回文子串(java)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...
- git 基础知识
git 分布式版本控制系统 git三棵树: 工作目录 红色 等待添加到暂存区域 需执行git add filename 命令添加到暂存区 暂存区域 绿色 文件等待被提交 需执行 git commit ...
- lanmp中环境变量的更改方法
1.vim /etc/profile 改成: export PATH=$PATH:/www/wdlinux/phps/71/bin/ 然后运行: source /etc/profile
- 1.1 从UNIX到Linux的发展历程
MIT的CTSS:第一个分时操作系统 ◼ Multics系统(Multiplexed Information and Computing System) ⚫ 1965年AT&T,MIT和GE的 ...
- ACM-选人问题(救济金发放)
n(n<20)个人站成一圈,逆时针编号为1-n.有两个官员,A从1开始逆时针数,B从n开 始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个 官员停在同一个人上) ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
- java调用url
1 try { String str; URL u = new URL("https://www.baidu.com"); InputStream is = u.openStrea ...