首页 > 其他 > 详细

LeetCode-Longest Common Prefix

时间:2016-04-05 07:06:45      阅读:262      评论:0      收藏:0      [点我收藏+]
Write a function to find the longest common prefix string amongst an array of strings.
public class Solution {
    public String longestCommonPrefix(String[] strs) {
    if(strs==null && strs.length()==0){
      return "";
    }
    int len=strs.length();
String pre=strs[0]; for(int i=1; i<len; i++){ int j=0; while(j<pre.length() && j<strs[i].length() && strs[i].charAt(j)==pre.charAt(j)){ j++; } if(j==0){ return ""; } pre=pre.substring(0, j); } return pre; } }

 

LeetCode-Longest Common Prefix

原文:http://www.cnblogs.com/incrediblechangshuo/p/5353640.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!