My solution with cpp。


  • 1
    Z

    class Solution {

    public:

    int lengthOfLastWord(string s) {
         int  len   = 0;
         char flag = 0;
         for(int i = s.length() - 1;i >= 0;i--)
         {
            if(s[i] != ' ')
            {
                len++;
                flag = 1;
            }
            else if(s[i] == ' ' && flag == 1)
            {
                break;
            }
         }
         return len;
    }
    

    };


Log in to reply
 

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