赞
踩
一、题目
二、解题思路
(1)思路一
数组中的所有字符串同时从第一个字符开始进行比较,如果都相等,复制到结果中,如果不相等,退出循环。第一个字符遍历完,需判断数组是否越界,表示某个字符串已经遍历完,结果中存的字符串就是最长前缀,退出循环。
三、VS-2017-测试代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #define StrArrGroupNum 4 #define StrArrNum 3 char* longestCommonPrefix(char** strs, int strsSize); char* CompareTwoStrCommon(char* str1, char* str2); void InitStr(char* strarr, int strarrlen); void CopyChar(char* str1, char str2); void CopyString(char* str1, char* str2); char* longestCommonPrefix_v1(char** strs, int strsSize); void main() { char* StrArr[StrArrGroupNum][StrArrNum] = { { "flower", "flow", "flight" }, { "dog","racecar","car" } , {"a","a","b"}, {"aac","a","ccc"}}; int i,j; for ( i = 0; i < StrArrGroupNum; i++) { for ( j = 0; j < StrArrNum; j++) { printf("%s ", StrArr[i][j]); } printf("res : %s\n",longestCommonPrefix(StrArr[i], StrArrNum)); printf("res : %s\n", longestCommonPrefix_v1(StrArr[i], StrArrNum)); printf("===============================\n\n"); } } /* 1、同时比较所有字符串。 2、各字符相等的话,把字符复制到结果中。 3、各字符相等的话,直接返回结果。 */ char* longestCommonPrefix_v1(char** strs, int strsSize) { char tmp = strs[0][0]; int res_size = 105; char* res = (char*)malloc(sizeof(char) * res_size); int i = 0; int j = 0; int flg = 0;//1:表示字符有不相等的情况出现。0:表示字符没有不相等的情况出现。 memset(res, '\0', res_size); while (1) { //printf("tmp : %c, res : %s\n",tmp,res); for (i = 1; i < strsSize; i++) { if (tmp != strs[i][j]) { flg = 1; break; } } if (flg) { break; } memcpy(res + strlen(res), &tmp, 1); j++; //判断数组是否越界 for (i = 0; i < strsSize; i++) { if (strlen(strs[i]) <= j) { flg = 1; break; } } if (flg) { break; } tmp = strs[0][j]; } return res; } char* longestCommonPrefix(char** strs, int strsSize) { if(strsSize == 1) { return strs[0]; } int i; int res_size = 200; char* res = (char*)malloc(sizeof(char) * res_size); char* tmp = (char*)malloc(sizeof(char) * res_size); size_t tmp_res_size = 0; //初始化数组 InitStr(res, res_size); InitStr(tmp, res_size); for (i = 1; i < strsSize; i++) { printf("\nstr1 : %s, str2 : %s, tmp_res_size : %zd\n",strs[0], strs[i], tmp_res_size); CopyString(tmp,CompareTwoStrCommon(strs[0], strs[i])); if(strlen(tmp) == 0) { return tmp; } else if(tmp_res_size == 0) { CopyString(res, tmp); tmp_res_size = strlen(tmp); } else if(strlen(CompareTwoStrCommon(res, tmp)) == 0) { return ""; } else if (tmp_res_size > strlen(tmp)) { CopyString(res, tmp); tmp_res_size = strlen(res); } } return res; } //实现的是最长公共前缀 char* CompareTwoStrCommon(char* str1, char* str2) { int res_size = 200; char* res = (char*)malloc(sizeof(char) * res_size); int i = 0; size_t str1_len = strlen(str1); size_t str2_len = strlen(str2); if (str1_len == 0 || str2_len == 0) { return ""; } //初始化数组 InitStr(res, res_size); while (str1[i] != '\0' && str2[i] != '\0') { if (str1[i] == str2[i]) { CopyChar(res, str1[i]); } else if (str1[i] != str2[i]) { break; } printf("str1[%d] : %c, str2[%d] : %c, res : %s\n", i, str1[i], i, str2[i], res); i++; } printf("++++++++++++++++\n"); return res; } /* //实现的是最长公共字符串 char* CompareTwoStrCommon(char* str1, char* str2) { int res_size = 200; char* res = (char*)malloc(sizeof(char) * res_size); int i = 0; int flg = 0;//两个字符串中是否出现相同的字符,出现:1,没有出现:0。 size_t str1_len = strlen(str1); size_t str2_len = strlen(str2); if (str1_len == 0 || str2_len == 0) { return ""; } //初始化数组 InitStr(res, res_size); while (str1[i] != '\0' && str2[i] != '\0') { if (str1[i] == str2[i]) { CopyChar(res, str1[i]); flg = 1; } else if(str1[i] != str2[i] && flg) { break; } printf("str1[%d] : %c, str2[%d] : %c, flg : %d,res : %s\n", i, str1[i], i, str2[i], flg, res); i++; } printf("++++++++++++++++\n"); return res; } */ void InitStr(char* strarr, int strarrlen) { int i; for (i = 0; i < strarrlen; i++) { strarr[i] = '\0'; } } void CopyChar(char* str1, char str2) { memcpy(str1 + strlen(str1), &str2, 1); } void CopyString(char* str1, char* str2) { memcpy(str1, str2, strlen(str2)+1); }
四、VS-2017代码测试截图
五、leecode提交代码
(1)版本一
char* longestCommonPrefix(char** strs, int strsSize); char* CompareTwoStrCommon(char* str1, char* str2); void InitStr(char* strarr, int strarrlen); void CopyChar(char* str1, char str2); void CopyString(char* str1, char* str2); char* longestCommonPrefix(char** strs, int strsSize) { if(strsSize == 1) { return strs[0]; } int i; int res_size = 200; char* res = (char*)malloc(sizeof(char) * res_size); char* tmp = (char*)malloc(sizeof(char) * res_size); size_t tmp_res_size = 0; //初始化数组 InitStr(res, res_size); InitStr(tmp, res_size); for (i = 1; i < strsSize; i++) { //printf("\nstr1 : %s, str2 : %s, tmp_res_size : %zd\n",strs[0], strs[i], tmp_res_size); CopyString(tmp,CompareTwoStrCommon(strs[0], strs[i])); if(strlen(tmp) == 0) { return tmp; } else if(tmp_res_size == 0) { CopyString(res, tmp); tmp_res_size = strlen(tmp); } else if(strlen(CompareTwoStrCommon(res, tmp)) == 0) { return ""; } else if (tmp_res_size > strlen(tmp)) { CopyString(res, tmp); tmp_res_size = strlen(res); } } return res; } //实现的是最长公共前缀 char* CompareTwoStrCommon(char* str1, char* str2) { int res_size = 200; char* res = (char*)malloc(sizeof(char) * res_size); int i = 0; size_t str1_len = strlen(str1); size_t str2_len = strlen(str2); if (str1_len == 0 || str2_len == 0) { return ""; } //初始化数组 InitStr(res, res_size); while (str1[i] != '\0' && str2[i] != '\0') { if (str1[i] == str2[i]) { CopyChar(res, str1[i]); } else if (str1[i] != str2[i]) { break; } //printf("str1[%d] : %c, str2[%d] : %c, res : %s\n", i, str1[i], i, str2[i], res); i++; } //printf("++++++++++++++++\n"); return res; } /* //实现的是最长公共字符串 char* CompareTwoStrCommon(char* str1, char* str2) { int res_size = 200; char* res = (char*)malloc(sizeof(char) * res_size); int i = 0; int flg = 0;//两个字符串中是否出现相同的字符,出现:1,没有出现:0。 size_t str1_len = strlen(str1); size_t str2_len = strlen(str2); if (str1_len == 0 || str2_len == 0) { return ""; } //初始化数组 InitStr(res, res_size); while (str1[i] != '\0' && str2[i] != '\0') { if (str1[i] == str2[i]) { CopyChar(res, str1[i]); flg = 1; } else if(str1[i] != str2[i] && flg) { break; } printf("str1[%d] : %c, str2[%d] : %c, flg : %d,res : %s\n", i, str1[i], i, str2[i], flg, res); i++; } printf("++++++++++++++++\n"); return res; } */ void InitStr(char* strarr, int strarrlen) { int i; for (i = 0; i < strarrlen; i++) { strarr[i] = '\0'; } } void CopyChar(char* str1, char str2) { memcpy(str1 + strlen(str1), &str2, 1); } void CopyString(char* str1, char* str2) { memcpy(str1, str2, strlen(str2)+1); }
(2)版本二
char* longestCommonPrefix(char** strs, int strsSize) { char tmp = strs[0][0]; int res_size = 105; char* res = (char*)malloc(sizeof(char) * res_size); int i = 0; int j = 0; int flg = 0;//1:表示字符有不相等的情况出现。0:表示字符没有不相等的情况出现。 memset(res,'\0',res_size); while (1) { //printf("tmp : %c, res : %s\n",tmp,res); for (i = 1; i < strsSize; i++) { if (tmp != strs[i][j]) { flg = 1; break; } } if (flg) { break; } memcpy(res + strlen(res), &tmp, 1); j++; //判断数组是否越界 for ( i = 0; i < strsSize; i++) { if (strlen(strs[i]) <= j) { flg = 1; break; } } if (flg) { break; } tmp = strs[0][j]; } return res; }
六、leecode提交截图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。