Re: [閒聊] 每日LeetCode已回收
https://leetcode.com/problems/is-subsequence/description
392. Is Subsequence
給你一個字串 s 和一個字串 t,求出字串 s 是否是 t 的子序列。
思路:
1.左邊指標指向s,右邊指標不斷往右並和s比較,如果左邊指標走到底就返回 true。
Java Code:
--------------------------------------
class Solution {
public boolean isSubsequence(String s, String t) {
if (s.length() == 0) {
return true;
}
int index = 0;
for (int i = 0; i < t.length(); i++) {
if (s.charAt(index) == t.charAt(i)) {
index++;
}
if (index == s.length()) {
return true;
}
}
return false;
}
}
--------------------------------------
--
https://i.imgur.com/YPBHGGE.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1695374229.A.4CC.html
→
09/22 17:23,
2年前
, 1F
09/22 17:23, 1F
→
09/22 17:23,
2年前
, 2F
09/22 17:23, 2F
→
09/22 17:25,
2年前
, 3F
09/22 17:25, 3F
→
09/22 17:25,
2年前
, 4F
09/22 17:25, 4F
討論串 (同標題文章)
完整討論串 (本文為第 416 之 719 篇):