My concise c++ solution


  • 0
    L
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int i = s.size();
            int len = 0;
            while(i--){
                if(s[i] != ' ')
                    ++len;
                else if(len > 0)
                    break;
                // if s[i] == ' ' and len == 0, skip trailing spaces
            }
            return len;
        }
    };

Log in to reply
 

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