Write a function to find the longest common prefix string amongst an array of strings.
char* longestCommonPrefix(char** strs, int strsSize) { if(strsSize==0) return ""; char* ret = strs[0]; int i, j; int cmpLen; for(int i = 1; i < strsSize; i++){ //traverse strs j = 0; cmpLen = (strlen(strs[i]) > strlen(ret))?strlen(ret):strlen(strs[i]); while(j < cmpLen && strs[i][j]==ret[j]) j++; ret[j] = ‘\0‘; } return ret; }
时间: 2024-10-17 14:54:16