LeetCode 14: Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
代码
C语言版本:1234567891011121314151617181920212223char* longestCommonPrefix(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] = '\0'; return res;}
117 / 117 test cases passed.
Status: Accepted
Runtime: 0 ms