char * longestCommonPrefix(char ** strs, int strsSize) {
char *ans = (char *)malloc(sizeof(char));
ans[0] = 0;
if (strs == NULL || strsSize == 0) {
return ans;
}
ans = (char *)malloc(sizeof(char) * (strlen(strs[0]) + 1));
memcpy(ans, strs[0], strlen(strs[0]) + 1);
int i = 0;
bool isDiff = false;
while (strs[0][i]) {
for (int j = 1; j < strsSize; j++) {
if (strs[j][i] != strs[0][i]) {
isDiff = true;
break;
}
}
if (isDiff) {
break;
}
i++;
}
ans[i] = 0;
return ans;
}
leetcode.14. 最长公共前缀
最新推荐文章于 2024-11-01 15:19:21 发布