Leetcode_ Longest public prefix

Source: Internet
Author: User

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

Key: The given function is:char* longestcommonprefix (char** strs, int strssize) where the parameter char** STRs represents the string number, int Strssize indicates how many strings there are

The requirement of the title is to find the longest common prefix in this strssize string, such as strssize=3, when the string is:

You can see that the common prefix for the string is ABC, but there are 1 extreme cases where the segment string is empty and returns "".

The code is as follows:

intMinintAintb) {    returnA<b?a:b;}Char* Longestcommonprefix (Char* * STRs,intstrssize) {    if(strssize==0)return ""; intLen=strlen (strs[0]); Char* s=strs[0];  for(intI=0; i<strssize-1; i++)    {        intTemp=min (Len,strlen (strs[i+1])); if(len>temp) len=temp; intJ;  for(j=0; j<len;j++)        {            if(s[j]!=strs[i+1][J]) Break; } Len=J; }    Char*s_out=malloc((len+1)*sizeof(Char));  for(intI=0; i<len;i++) {S_out[i]=S[i]; } S_out[len]=' /'; returns_out;}

Here is the test results, you can see near the speed of 0ms to kill other languages, C language in the operating speed is still very strong.

Leetcode_ 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.