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

Appearance settings

Latest commit

 

History

History
History
31 lines (25 loc) · 886 Bytes

File metadata and controls

31 lines (25 loc) · 886 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Algorithms.string;
public class LongestCommonPrefix {
//http://blog.csdn.net/fightforyourdream/article/details/14642079
public String longestCommonPrefix(String[] strs) {
if (strs == null) {
return null;
}
if (strs.length == 0) {
return "";
}
String s = strs[0];
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
for (int j = 1; j < strs.length; j++) {
if (strs[j].length() <= i || c != strs[j].charAt(i)) {
// The char i is invalid. 因为读到i时退出,所以不应包含i本身。
return s.substring(0, i);
}
}
}
// Didn't break, the whole String is valid.
return s;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.