当前位置:网站首页>【代码随想录-动态规划】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;
}
边栏推荐
- Tke accesses the cluster through kubectl in pod
- Grp: how to add Prometheus monitoring in GRP service?
- ClickHouse Buffer
- Grpc: how to make grpc provide swagger UI?
- Concise and practical time code
- Tencent location service appeared at the 11th China Surveying and mapping Geographic Information Technology Equipment Expo
- The importance of the computer room to the stable operation of the server
- Simple and beautiful weather code
- How do I check the trademark registration number? Where do I need to check?
- What aspects does the intelligent identification system include? Is the technology of intelligent identification system mature now?
猜你喜欢
Thank you for your recognition! One thank-you note after another

元气森林推“有矿”,农夫山泉们跟着“卷”?

QT creator tips
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis
![[summary of interview questions] zj5](/img/d8/ece82f8b2479adb948ba706f6f5039.jpg)
[summary of interview questions] zj5

Community pycharm installation visual database

Get to know MySQL database

618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?

Simple and beautiful weather code

On Sunday, I rolled up the uni app "uview excellent UI framework"
随机推荐
What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
How to select a server with appropriate configuration when planning to build a live broadcast platform
Why can't the fortress machine open the port? There is a problem with the use of the fortress machine port
web渗透测试----5、暴力破解漏洞--(7)MYSQL密码破解
How to solve the problem of easycvr playing the total recording time in the specified time period?
What is the principle of intelligent image recognition? What are the applications of intelligent image recognition?
Three Scheduling Strategies in yarn
Chapter 5: key led demo case of PS bare metal and FreeRTOS case development
How to install CentOS 6.5 PHP extension
Summary of common problems of real-time audio and video TRTC - quality
What is fortress resource authorization? What is barrier machine?
Mocktio usage (Part 2)
Which domestic cloud desktop server is good? What are the security guarantees for cloud desktop servers?
What is an edge calculator? How is the unit price of the edge calculator calculated?
Thank you for your recognition! One thank-you note after another
Big coffee face to face | Dr. Chen Guoguo talks about intelligent voice
Coding CD of Devops
What is the difference between server leasing and hosting?
Why use code signing? What certificates are required for code signing?
Process kill problem