当前位置:网站首页>Sword finger offer II 095 Longest common subsequence
Sword finger offer II 095 Longest common subsequence
2022-06-25 06:17:00 【Normal male human】
Given two strings text1 and text2, Returns the longest of these two strings Common subsequence The length of . If it doesn't exist Common subsequence , return 0 .
A string of Subsequence This is a new string : It is to delete some characters from the original string without changing the relative order of the characters ( You can also delete no characters ) After the composition of the new string .
for example ,“ace” yes “abcde” The subsequence , but “aec” No “abcde” The subsequence .
Two strings of Common subsequence Is the subsequence shared by these two strings .
Example 1:
Input :text1 = “abcde”, text2 = “ace”
Output :3
explain : The longest common subsequence is “ace” , Its length is 3 .
Example 2:
Input :text1 = “abc”, text2 = “abc”
Output :3
explain : The longest common subsequence is “abc” , Its length is 3 .
Example 3:
Input :text1 = “abc”, text2 = “def”
Output :0
explain : The two strings have no common subsequence , return 0 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/qJnOS7
Dynamic programming :
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int m = text1.length(), n = text2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
char c1 = text1.charAt(i - 1);
for (int j = 1; j <= n; j++) {
char c2 = text2.charAt(j - 1);
if (c1 == c2) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
}
dp[i][j] The meaning of :text1 Of 0 ~ i String and text2 Of 0 ~ j The common subsequence length of the string of .
First, we can determine the boundary, that is dp[i][0] and dp[0][i] = 0, Because the length is 0 The length of the common subsequence of the string must also be 0.
Because array initialization is 0, So you don't have to initialize the boundary again .
When subscript i And j Situated The letters are the same , explain dp[i][j] = dp[i-1][j-1] + 1,+1 Refer to subscript i and j Can be directly added to the longest common subsequence .
If the letters are different , be Compare dp[i-1][j] and dp[i][j-1] In both cases , Take the larger .
From a two-dimensional perspective , Whether it's dp[i-1][j] still dp[i][j-1] All in dp[i][j] Top left of , So we have i It is an external heavy cycle j Traverse for inner loop , You can finish the whole dp The calculation of array .
Eventually return dp[m][n].
边栏推荐
- MySQL uses the where condition to find strange results: solve
- CST8227
- Leetcode sword finger offer question brushing - day 27
- How SAP ui5 device type detection device API works
- DF command – displays disk space usage
- PHP and WMI – explore windows with PHP
- Advantages and disadvantages of using SNMP and WMI polling
- SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
- Day22 send request and parameterization using JMeter
- Report on the application prospect and investment potential of global and Chinese cell therapy industry 2022-2028
猜你喜欢

What is the slice flag bit
SAP ui5 date type sap ui. model. type. Analysis of date parsing format

Soft exam information system project manager_ Management Science (Operations Research) 2--- senior information system project manager of soft test 034
MySQL transaction learning notes (I) first encounter

Mongodb basic concept learning - Documentation

【LeetCode】40. Combined summation II (2 strokes of wrong questions)

General test point ideas are summarized and shared, which can be directly used in interview and actual software testing
![[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management](/img/62/dab2ac0526795f2040394acd9efdd3.jpg)
[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
![[open source sharing] deeply study KVM, CEPH, fuse features, including open source projects, code cases, articles, videos, architecture brain maps, etc](/img/9d/9bcf52f521e92cf97eb1d545931c68.jpg)
[open source sharing] deeply study KVM, CEPH, fuse features, including open source projects, code cases, articles, videos, architecture brain maps, etc
随机推荐
Global and Chinese gallium nitride (GAN) market output value scale forecast and application prospect analysis report 2022
Forecast report on output demand and supply scale of global and Chinese structural ceramics market for semiconductor equipment (2022 Edition)
Day21 performance test process
Global and Chinese kaolin market operation scale and investment development proposal report 2022
Distributed solar photovoltaic inverter monitoring
Notes on dashboard & kuboard installation in kubernetes cluster
Analysis report on production and sales demand and sales prospect of global and Chinese phosphating solution Market 2022-2028
Jz-066- motion range of robot
Analysis report on global and Chinese pharmaceutical excipients industry competition and marketing model 2022-2028
[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management
Aiot project that is an introduction to the basics of the Internet of things and can be implemented in practice
Handling skills of SQL optimization (2)
Soft exam information system project manager_ Management Science (Operations Research) -- senior information system project manager of soft test 033
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
Report on strategic suggestions on investment direction and Prospect of global and Chinese marine biological industry (2022 Edition)
Differences and connections between sap ui5 and openui5
SAP ui5 date type sap ui. model. type. Analysis of date parsing format
MySQL transaction learning notes (I) first encounter
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
MySQL tuning --01--- optimization steps and system performance parameters