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

Commite9160df

Browse files
committed
longest
1 parentb6f4e08 commite9160df

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

‎string/LongestCommonPrefix.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packageAlgorithms.string;
2+
3+
publicclassLongestCommonPrefix {
4+
//http://blog.csdn.net/fightforyourdream/article/details/14642079
5+
publicStringlongestCommonPrefix(String[]strs) {
6+
if (strs ==null) {
7+
returnnull;
8+
}
9+
10+
if (strs.length ==0) {
11+
return"";
12+
}
13+
14+
Strings =strs[0];
15+
intlen =s.length();
16+
17+
for (inti =0;i <len;i++) {
18+
charc =s.charAt(i);
19+
20+
for (intj =1;j <strs.length;j++) {
21+
if (strs[j].length() <=i ||c !=strs[j].charAt(i)) {
22+
// The char i is invalid. 因为读到i时退出,所以不应包含i本身。
23+
returns.substring(0,i);
24+
}
25+
}
26+
}
27+
28+
// Didn't break, the whole String is valid.
29+
returns;
30+
}
31+
}

‎string/isMatch.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@ public boolean isMatchRec(String s, String p, int indexS, int indexP) {
1818
returnindexS ==lenS;
1919
}
2020

21-
// only 1 match character left.
22-
if (indexP ==lenP -1) {
23-
// String should also have 1 character left.
24-
returnindexS ==lenS -1 &&isMatchChar(s.charAt(indexS),p.charAt(indexP));
25-
}
26-
2721
// At lease 2 match character left
28-
if (p.charAt(indexP +1) =='*') {
22+
if (indexP <lenP -1 &&p.charAt(indexP +1) =='*') {
2923
// match 0;
3024
if (isMatchRec(s,p,indexS,indexP +2)) {
3125
returntrue;
@@ -65,4 +59,6 @@ public boolean isMatchChar(char s, char p) {
6559
returnfalse;
6660
}
6761

62+
}
63+
6864
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp