Why is wrong? In Visual C++, this is right.


  • 0
    D

    class Solution {
    public:
    int lengthOfLastWord(string s) {
    int n = s.length();
    int count = 0;
    for (int i = n - 1; i >= 0 && s[i] != ' '; i--){
    count++;
    }
    return count;
    }
    };

    Why this is wrong. I have tried it in visual c++. In it, this is right.


  • 0
    Y

    You solution may fail for "a ", btw you should format your code using ```.


Log in to reply
 

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