My C++ solution, 4ms, 6 lines


  • 0
    T

    class Solution {
    public:
    int lengthOfLastWord(const char *s) {
    int length = strlen(s);
    while (s[length - 1] == ' ') length--;
    if (length == 0) return 0;
    for (int i = length - 1; i >= 0; i--)
    if (s[i] == ' ') return length - i - 1;
    return length;
    }
    };


Log in to reply
 

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