Why my c++ code get this weird result?


  • 0
    X

    My code as follow:

    int lengthOfLastWord(const char *s) {
        if(s == NULL) return 0;
        int i = -1;
        int count = 0;
        while(s[++i] == ' ');
    
        // 找到第一个不为' '
        while(s[i] != '\0')
        {
            count = 1;
            while(s[++i] != ' ' && s[i] != '\0')
            {
                ++count;
            }
            while(s[++i] == ' ');
        }
        return count;
    }
    

    Input: " vboImaga"

    Output: 1

    Expected: 8

    But the answer is correct in my local environment with Apple LLVM version 6.0. Why?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.