Leetcode#14longest Common Prefix

Source: Internet
Author: User

Longest Common Prefix

Total Accepted: 44491 Total Submissions: 170999 My Submissions

Question Solution


Write a function to find the longest common prefix string amongst an array of strings.


Show Tags

Parsing, the longest common prefix, first finds the shortest string length, as a metric size, and then sequentially parses each character to see if the characters are the same, and the same is added to the final result set, different stops, returns the final result

public class Solution {

Public String Longestcommonprefix (string[] strs) {

int count=strs.length;

String nullstr= "";

if (count==0)

return nullstr;

if (count==1)

return strs[0];

int Ml=strs[0].length ();

for (int i=1;i<count;i++)

{

int L=strs[i].length ();

if (L<ML)

Ml=l;

}

if (ml==0)

return nullstr;

Else

{

String s= "";

int j=0;

for (int i=0;i<ml;i++)

{

for (j=1;j<count;j++)

if (Strs[j].charat (i)!=strs[j-1].charat (i))

Break

if (J!=count)

Break

Else

S=s+strs[j-1].charat (i);

}

return s;

}

}

}


Leetcode#14longest Common 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.