My 3ms solution, 3ms


  • 0
    E
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int leng = 0;
            int size = s.size();
            while(s[size-1] == ' '){
                size--;
            }
            for (int i = size-1; i >= 0; i--){
                if(s[i] == ' ')
                    return leng;
                else
                    leng++;
            }
            return leng;
        }
    };
    

Log in to reply
 

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