題目:
Write a function to find the longest common prefix string amongst an array of strings.
題解:
解題思路是,先對整個String陣列預處理一下,求一個最小長度(最長字首肯定不能大於最小長度)。
然後以第0個字串作為參照,從第1個字串到最後一個字串,對同一位置做判斷,有不同字串返回當前記錄的字串就行。
我的程式碼如下,不是那麼簡潔好看,下面有個整理的更好一些:
1 private static int MinLength(String[] strs) {
2 int temp = Integer.MAX_VALUE;
3 for(int i=0; i<strs.length;i++){
4 if(temp>strs[i].length())
5 temp = strs[i].length();
6 }
7 return temp;
8 }
9
10 public static String longestCommonPrefix(String[] strs) {
11 if(strs.length==0){
12 return "";
13 }
14 int j = 0;
15 boolean flag = false;
16
17 int length = MinLength(strs);
18
19 while(j<length){
20 int i = 1;
21 while(i<strs.length){
22 int c = strs[0].charAt(j);
23 if(strs[i].charAt(j)==c){
24 i++;
25 }else{
26 flag = true;
27 break;
28 }
29 }
30 if(flag)
31 break;
32 j++;
33 }
34
35 return strs[0].substring(0, j);
36 }
2 int temp = Integer.MAX_VALUE;
3 for(int i=0; i<strs.length;i++){
4 if(temp>strs[i].length())
5 temp = strs[i].length();
6 }
7 return temp;
8 }
9
10 public static String longestCommonPrefix(String[] strs) {
11 if(strs.length==0){
12 return "";
13 }
14 int j = 0;
15 boolean flag = false;
16
17 int length = MinLength(strs);
18
19 while(j<length){
20 int i = 1;
21 while(i<strs.length){
22 int c = strs[0].charAt(j);
23 if(strs[i].charAt(j)==c){
24 i++;
25 }else{
26 flag = true;
27 break;
28 }
29 }
30 if(flag)
31 break;
32 j++;
33 }
34
35 return strs[0].substring(0, j);
36 }
更簡潔的程式碼:
1 private int minlen(String[] strs) {
2 int min = Integer.MAX_VALUE;
3 for(int i=0; i<strs.length;i++)
4 min = Math.min(min,strs[i].length());
5 return min;
6 }
7
8 public String longestCommonPrefix(String[] strs) {
9 if (strs == null || strs.length == 0)
10 return "";
11
12 StringBuilder res = new StringBuilder();
13 int index = 0;
14 int len = minlen(strs);
15 while (index < len){
16 for (int i=1; i<strs.length;i++){
17 if (strs[i].charAt(index) != strs[0].charAt(index))
18 return res.toString();
19 }
20 res.append(strs[0].charAt(index));
21 index++;
22 }
23 return res.toString();
24 }
2 int min = Integer.MAX_VALUE;
3 for(int i=0; i<strs.length;i++)
4 min = Math.min(min,strs[i].length());
5 return min;
6 }
7
8 public String longestCommonPrefix(String[] strs) {
9 if (strs == null || strs.length == 0)
10 return "";
11
12 StringBuilder res = new StringBuilder();
13 int index = 0;
14 int len = minlen(strs);
15 while (index < len){
16 for (int i=1; i<strs.length;i++){
17 if (strs[i].charAt(index) != strs[0].charAt(index))
18 return res.toString();
19 }
20 res.append(strs[0].charAt(index));
21 index++;
22 }
23 return res.toString();
24 }
Reference:http://blog.csdn.net/linhuanmars/article/details/21145733
-------------------
更新
空間複雜度更小的程式碼如下(from discussion):
1 public String longestCommonPrefix(String[] strs) {
2 if(strs.length == 0||strs == null)
3 return "";
4
5 for(int i = 0; i<strs[0].length(); i++){
6 char x = strs[0].charAt(i);
7 for(int j = 1; j<strs.length; j++){
8 if(strs[j].length() == i || strs[j].charAt(i) != x)
9 return strs[0].substring(0,i);
10 }
11 }
12
13 return strs[0];
14 }
2 if(strs.length == 0||strs == null)
3 return "";
4
5 for(int i = 0; i<strs[0].length(); i++){
6 char x = strs[0].charAt(i);
7 for(int j = 1; j<strs.length; j++){
8 if(strs[j].length() == i || strs[j].charAt(i) != x)
9 return strs[0].substring(0,i);
10 }
11 }
12
13 return strs[0];
14 }