2023每日刷题(三十八)
Leetcode—14.最长公共前缀
实现代码
char* longestCommonPrefix(char** strs, int strsSize) {char *ans = (char *)malloc(sizeof(char) * 203);char *p = ans;int len = 1, i = 1;int flag = 0;if(strsSize == 0) {return "";}if(strsSize == 1) {return *strs;}if(strlen(*(strs + 0)) == 0) {return "";}while(len <= 200) {for(i = 1; i < strsSize; i++) {if(strncmp(*(strs + 0), *(strs + i), len) != 0) {flag = 1;break;}}if(flag) {break;}strncpy(p, *(strs + 0), len);len++;}if(len == 1 && flag) {return "";}*(p + len - 1) = '\0';return ans;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!