500. Keyboard Row - Is it a keyboard row sequence

hhttps://leetcode.com/problems/keyboard-row/

analyze

Judge whether each given sequence can be composed of a line of letters in the keyboard. It is relatively simple to write and does not use any algorithm. It is simple string matching and judgment, and there is no split encapsulation function. It is more verbose and still used. With goto, there are a few things to pay attention to:

  1. The goto statement cannot end directly, at least one statement is required, otherwise an error will be reported
  2. Dynamic two-dimensional array memory application, first apply for row pointer, and then apply for column memory

accomplish

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** findWords(char** words, int wordsSize, int* returnSize) {
    int *pYesWordNum;
    int yesWordCount = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    int line1Flag = 0;
    int line2Flag = 0;
    int line3Flag = 0;
    int wordLen = 0;
    int maxWordLen = 0;
    int retWordNum = 0;
    char line1[20] = {'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
    char line2[18] = {'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
    char line3[14] = {'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
    char **pRetWords;
    int pp;

    pYesWordNum = (int *)malloc(wordsSize * sizeof(int));
    memset(pYesWordNum, 0, wordsSize * sizeof(int));

    for (i = 0; i < wordsSize; i++)
    {
        wordLen = (int)strlen(words[i]);
        if (wordLen > maxWordLen)
        {
            maxWordLen = wordLen;
        }

        line1Flag = 0;
        line2Flag = 0;
        line3Flag = 0;

        for (j = 0; j < wordLen; j++)
        {
            for (k = 0; k < 20; k++)
            {
                if (words[i][j] == line1[k])
                {
                    if ((line2Flag == 1) || (line3Flag == 1))
                    {
                        goto nextWord;
                    }
                    line1Flag = 1;
                    goto nextAlphabet;
                }
            }

            for (k = 0; k < 18; k++)
            {
                if (words[i][j] == line2[k])
                {
                    if ((line1Flag == 1) || (line3Flag == 1))
                    {
                        goto nextWord;
                    }
                    line2Flag = 1;
                    goto nextAlphabet;
                }
            }

            for (k = 0; k < 14; k++)
            {
                if (words[i][j] == line3[k])
                {
                    if ((line1Flag == 1) || (line2Flag == 1))
                    {
                        goto nextWord;
                    }
                    line3Flag = 1;
                    goto nextAlphabet;
                }
            }

            /* 该单词已检查,跳转下一个字母 */
            nextAlphabet:

            pp =1;
        }


        /* 该单词符合,进行记录 */
        pYesWordNum[i] = 1;

        /* 该单词不符合,跳转下一个单词 */
        nextWord:

        pp =1;
    }

    /* 统计符合单词的数目 */
    for (i = 0; i < wordsSize; i++)
    {
        if (pYesWordNum[i] == 1)
        {
            yesWordCount++;
        }
    }

    pRetWords = (char **)malloc(yesWordCount * sizeof(char *));
    for (i = 0; i < yesWordCount; i++) 
    {
        pRetWords[i]=(char *)malloc(maxWordLen);
    }

    for (i = 0; i < yesWordCount; i++)
    {
        memset(pRetWords[i], 0, maxWordLen);
    }

    for (i = 0; i < wordsSize; i++)
    {
        if (pYesWordNum[i] == 1)
        {
            strcpy(pRetWords[retWordNum], words[i]);
            retWordNum++;
        }
    }

    free(pYesWordNum);

    *returnSize = retWordNum;
    return pRetWords;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325563940&siteId=291194637
Row