[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. ...
随机推荐
- 最简单的解决Chrome浏览器主页被hao123、360和2345篡改的方法是什么
最简单的解决Chrome浏览器主页被hao123.360和2345篡改的方法是什么 一.总结 一句话总结:打开chrome的安装目录,将chrome.exe改成chrome1.exe即可,然后发送一个 ...
- HAL库详解
转自:https://blog.csdn.net/zcshoucsdn/article/details/55213616
- OnSen UI结合AngularJs打造”美团"APP"附近”页面 --Hybrid App
1.页面效果图: 演示链接地址:http://www.nxl123.cn/bokeyuan/meiTuanDemo_near/ 2.核心代码 near.html: <ons-page id=&q ...
- linux权限管理之文件属性
文件属性 chattr ======================================================== 文件权限管理之: 文件属性注:设置文件属性(权限),针对所有用 ...
- Rest_framework 和路由配置(一)
简介 Django REST framework是一个建立在Django基础之上的Web 应用开发框架,可以快速的开发REST API接口应用. Rest_framework 核心思想: 缩减代码. ...
- css 选择器二
2.4 盒模型 2.4.1 定义 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型 ...
- php值传递和引用传递
1,参数传值方式有两种,第一种是值传递,第二种引用传递.值传递比较简单,也就是在php中,数组是当一个普通变量,值传递是要一个实参的一个拷贝副本,跟实参无关,而引用传递后可以改变实参的值而类的对象是无 ...
- git:not a git repository (or any of the parent directories)
我用git add file添加文件时出现了这样错误: fatal: Not a git repository (or any of the parent directories): .git 提示说 ...
- hdu-1817-polya
Necklace of Beads Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- SQL 查询语句
4.2 单表查询 4.2.1 列名(表名)的别名(as 可以不加) 给列名取别名既可以加 as 也可以不加. (2008 - Sage.lower(Sdept)等可计算但无列名,需要指定列名) 原列名 ...