Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit386d1f0

Browse files
author
piggy1991
committed
long
1 parent0f59249 commit386d1f0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packageAlgorithms.lintcode.dp;
2+
3+
publicclassLongestCommonSubsequence {
4+
/**
5+
* @param A, B: Two strings.
6+
* @return: The length of longest common subsequence of A and B.
7+
*/
8+
publicintlongestCommonSubsequence(StringA,StringB) {
9+
// write your code here
10+
if (A ==null ||B ==null) {
11+
return0;
12+
}
13+
14+
intlenA =A.length();
15+
intlenB =B.length();
16+
int[][]D =newint[lenA +1][lenB +1];
17+
18+
for (inti =0;i <=lenA;i++) {
19+
for (intj =0;j <=lenB;j++) {
20+
if (i ==0 ||j ==0) {
21+
D[i][j] =0;
22+
}else {
23+
if (A.charAt(i -1) ==B.charAt(j -1)) {
24+
D[i][j] =D[i -1][j -1] +1;
25+
}else {
26+
D[i][j] =Math.max(D[i -1][j],D[i][j -1]);
27+
}
28+
}
29+
}
30+
}
31+
32+
returnD[lenA][lenB];
33+
}
34+
}
35+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp