392. Is Subsequence(JAVA)
来源:互联网 发布:开淘宝店保证金怎么交 编辑:程序博客网 时间:2024/06/07 05:01
題目
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 sis 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?
Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.
思路
兩層迴圈,外層迭代子串,內層迭代字符串。找出子串中的字母是否在字符串中,注意字符串中已被迭代過的部分不能再次迭代。
代碼
class Solution {
public boolean isSubsequence(String s, String t) {
int index = -1, i, j;
for (i = 0; i < s.length(); i++) {
for (j = index+1; j < t.length(); j++) {
if (s.charAt(i) == t.charAt(j)) {
index = j;
break;
}
}
if (j == t.length()) {
break;
}
}
if (i != s.length()) {
return false;
}
else {
return true;
}
}
}
感想
超級簡單的一道題……不是很能理解通過率為啥這麼低。
- 392. Is Subsequence(JAVA)
- 392. Is Subsequence
- 392.[LeetCode] Is Subsequence
- 392. Is Subsequence
- 392. Is Subsequence
- LeetCode 392. Is Subsequence
- 392. Is Subsequence
- 392. Is Subsequence
- [LeetCode] 392. Is Subsequence
- 【leetcode】392. Is Subsequence
- 【LeetCode】392. Is Subsequence
- 【LeetCode】392. Is Subsequence
- 392. Is Subsequence
- 392. Is Subsequence
- Leetcode 392. Is Subsequence
- 392. Is Subsequence
- 392. Is Subsequence
- leetcode 392. Is Subsequence
- 猜数游戏
- 功能测试工具SilkTest
- java小弟
- Response对象响应的中文乱码处理 Response向页面响应中文
- Java反射
- 392. Is Subsequence(JAVA)
- Java多线程(2)——并发访问控制
- 学习笔记 何为ARM与最小系统(基于恩智浦智能车的理解)
- mapper.xml配置文件里使用<无效
- MVC架构输出hello world
- 栈之应用---括号匹配
- 进程PPT重点
- java小弟
- 设计模式-单例模式