Fork me on GitHub

每日一题:Longest-Common-Prefix

题目

Write a function to find the longest common prefix string amongst an array of strings.

解答

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
32
33
34
35
36
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0){
return "";
}

int index = 0;
StringBuilder sb = new StringBuilder();
boolean flag = true;

while (true) {
char pre;
if(index < strs[0].length()){
pre = strs[0].charAt(index);
}else{
flag = false;
break;
}
for(int i = 0; i < strs.length; i++){
if(index < strs[i].length() && strs[i].charAt(index) == pre){
pre = strs[i].charAt(index);
}else{
flag = false;
break;
}
}
if(flag == true){
sb.append(pre);
index++;
}else{
break;
}
}
return sb.toString();
}
}

注解

遍历每个数组的每一位的字符值,比较是否相等。相等就继续匹配下一位的字符比较。通过对边界值的判断来跳出循环。