【LeetCode从零单排】No14.LongestCommonPrefix

题目

    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.length==0) return "";
        if(strs.length==1) return strs[0];
		   String commonPrefix="";
	       boolean end=true;
	      
	       char strChar[]=strs[0].toCharArray();
	        ok:
	        for (int j = 0;j<strChar.length;j++){
	        	
	    	     for(int i=1;i<strs.length;i++){
	    	    	   char tempChar[]=strs[i].toCharArray();
	    	    	   int tempChar_length=tempChar.length;
	    	    	   if(j<tempChar_length){
	    	    	     if(tempChar[j]==strChar[j]){
	    	    		   
	    	    	      }
	    	    	     else{
	    	    		   
	    	    		   break ok; 
	    	    	     
	    	    	      }
	    	    	     }
	    	    	   else{
	    	    		   break ok;
	    	    	   }
	    	    	   
	    	    	   
	          } 
	          commonPrefix+=strChar[j];
		    }
	        
	       return commonPrefix;
    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode


/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/



你可能感兴趣的:(java,LeetCode)