C++ solution


  • 0
    R

    Iterate from the end of the string until the first ' ' is found.
    """
    class Solution {
    public:
    int lengthOfLastWord(string s)
    {
    if (s.length() < 1) return 0;
    int count = 0;
    for (int i = s.length()-1; i>=0; i--)
    {
    if (s[i] != ' ')
    count++;
    else if (count > 0)
    break;
    }
    return count;
    }
    };
    """


Log in to reply
 

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