Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

这个代码哪错了啊?题目和错误提示如下:

char * longestCommonPrefix(char ** strs, int strsSize){
    char *a = malloc(sizeof(char) * 10000);
    int j = 1, z = 0;
    if (strsSize == 0)
        return "";
    if (strsSize == 1)
        return strs[0];
    for (int i = 0; i < strlen(strs[0][i]); ++i) {
        while (strs[j][i] && strs[j - 1][i] == strs[j][i]) {
            j++;
        }
        if (j == strsSize) {
            j = 1;
            a[z++] = strs[0][i];
        }
        else
            break;
    }
    a[z] = '';
    return a;      
}

imageimage


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你返回了一个局部变量 a 。你 malloc 一下吧。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...