标签:leetcode 字符串 字符串数组 错误 char for not match 问题
之前一直忽略了一个问题就是:给定的空字符串数组
1 char* longestCommonPrefix(char** strs, int strsSize) { 2 char* result; 3 if(strsSize == 0) { 4 result = (char *)malloc(sizeof(char) * 1); 5 result[0] = ‘\0‘; 6 return result; 7 } 8 9 int i=0; 10 while(strs[0][i] != NULL && strs[0][i] != ‘\0‘) { 11 i++; 12 } 13 14 int len = i==0 ? 2:i+1; 15 result = (char *)malloc(sizeof(char) * len); 16 17 for(i=0; i<len; i++) { 18 char t = strs[0][i]; /* get first character of first string */ 19 if(t == ‘\0‘) { 20 result[i] = ‘\0‘; 21 return result; 22 } 23 int flag = 1; 24 int j=0; 25 for(j=0; j<strsSize; j++) /* traverse strings */ 26 if(strs[j][i] != t) { 27 flag = 0; /* if not match then */ 28 t = ‘\0‘; 29 break; 30 } 31 result[i] = t; 32 if(flag==0) { 33 break; 34 } 35 } 36 return result; 37 }
还有遇到的错误是
empty character constant
1 char t = ‘‘;
这种写法在C语言中是错误的
标签:leetcode 字符串 字符串数组 错误 char for not match 问题
原文地址:http://www.cnblogs.com/tuhooo/p/7657303.html