Write a function to find the longest common prefix string amongst an array of strings.
代码
C语言版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
char* (char** strs, int strsSize) { if (strs == NULL || *strs == NULL) { return ""; } int len = strlen(strs[0]); int max = 0; char *res; while (max < len) { for (int i = 1; i < strsSize; i++) { if (strs[i][max] != strs[0][max]) { len = max; break; } } if (max < len) max++; } res = (char*)malloc(sizeof(char)*(max+1)); for (int i = 0; i < max; i++) { res[i] = strs[0][i]; } res[max] = '
|
近期评论