当前位置:网站首页>【代码随想录-动态规划】T392.判断子序列
【代码随想录-动态规划】T392.判断子序列
2022-06-24 03:34:00 【不写博客就不爽】
T392、判断子序列(6/23)
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
进阶:
如果有大量输入的 S,称作 S1, S2, … , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/is-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题目跟求解最长子序列思路一样,我按照最长子序列的代码提交了一次,结果正确。
后来参考代码随想录思路,发现一个位置有差异,就是当
s.charAt(i) != s.charAt(j)
这种情况下,我们就不能像原先最长子序列那样求 dp[i][j] = max(dp[i-1][j], dp[i][j-1])
而是 dp[i][j] = dp[i][j-1]
因为这个时候 S 的子串[0:i-1] 已经不在 T[0:j-1] 中,所以此时相当于t要删除元素,t如果把当前元素t[j - 1]删除,那么dp[i][j] 的数值就是 看s[i - 1]与 t[j - 2]的比较结果了,即:dp[i][j] = dp[i][j - 1];
dp
public boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
int[][] dp = new int[n+1][m+1];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(s.charAt(i-1) == t.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = dp[i][j-1];
}
}
}
return dp[n][m] == s.length()? true:false;
}
双指针法

public boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
int i = 0, j =0;
while(i < n && j < m){
if(s.charAt(i) == t.charAt(j)){
i++;
}
j++;
}
return i == n?true:false;
}
边栏推荐
- Highlights of future cloud native CIF Forum
- Record the creation process of a joke widget (I)
- A figure showing the price and expense structure of Tencent cloud real-time audio and video TRTC
- Understanding Devops from the perspective of decision makers
- Community pycharm installation visual database
- Coding Ci of Devops
- What are the responsibilities of cloud desktop administrators? How to use cloud desktop?
- Which domestic cloud desktop server is good? What are the security guarantees for cloud desktop servers?
- web渗透测试----5、暴力破解漏洞--(7)MYSQL密码破解
- What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
猜你喜欢

QT creator tips

On Sunday, I rolled up the uni app "uview excellent UI framework"

Community pycharm installation visual database

Get to know MySQL database

Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis
Thank you for your recognition! One thank-you note after another
![[summary of interview questions] zj5](/img/d8/ece82f8b2479adb948ba706f6f5039.jpg)
[summary of interview questions] zj5

Simple and beautiful weather code

618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
随机推荐
[new double 11] the latest interpretation of Tencent cloud double 11! Get 11000 yuan voucher now!!
How does cloud computing achieve elastic scaling? What are the characteristics of elasticity?
2021-10-02: word search. Given an M x n two-dimensional character grid boa
[competition experience sharing] design of intelligent guide rod
What is an edge calculator? How is the unit price of the edge calculator calculated?
Supply chain system platform: two management areas
The request was aborted: Could not create SSL/TLS secure channel.
Troubleshooting and resolution of errors in easycvr calling batch deletion interface
Under what circumstances do you need a fortress machine? What are the functions of a fortress machine
Tencent location service appeared at the 11th China Surveying and mapping Geographic Information Technology Equipment Expo
Elk7.15.1 installation, deployment and construction
Grpc: how to add API log interceptors / Middleware?
Several key tools for cloud native implementation
Process kill problem
Hunan data security governance Summit Forum was held, and Tencent built the best practice of government enterprise data security
What port does the fortress machine use? What is the role of the fortress machine?
Tke accesses the cluster through kubectl in pod
13. Tencent cloud IOT device side learning - data template function and Implementation
Grpc: how to implement distributed log tracing?
web渗透测试----5、暴力破解漏洞--(2)SNMP密码破解