IncredibleThings

导航

LeetCode-Longest Common Prefix

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; } }

 

posted on 2016-04-05 05:59  IncredibleThings  阅读(141)  评论(0编辑  收藏  举报