Longest Common Prefix leetcode java

题目:

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         }

 

更简洁的代码:

 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     }

 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     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值