"Leetcode-Interview algorithm classic-java Implementation" "014-longest Common Prefix (longest public prefix)"

Source: Internet
Author: User

"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)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.