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