"014-longest Common Prefix (longest public prefix)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Write a function to find the longest common prefix string amongst an array of strings.
Main Topic
Write a function to find the longest common prefix in the array of strings.
Thinking of solving problems
The first step is to find the string with the smallest length, and then compare the string to its string to find the shortest common prefix.
Code Implementation
Public class solution { PublicString Longestcommonprefix (string[] strs) {if(STRs = =NULL) {return NULL; }if(Strs.length = =0) {return ""; }intmin = Integer.max_value;//The length of the shortest string //Find the length of the short string for(StringStr: STRs) {if(Str==NULL) {return NULL; }if(Min >Str. Length ()) {min =Str. Length (); } }intI//Record the maximum number of characters in a prefix BooleanFlag for(i =0; i < min; i++) {flag =true; for(intj =1; J < Strs.length; J + +) {if(strs[0].charat (i)! = Strs[j].charat (i)) {flag =false; Break; } }if(!flag) { Break; } }//if (i = = 0) {//return null;// } returnstrs[0].substring (0, i); }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46963385"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "014-longest Common Prefix (longest public prefix)"