4 lines concise and easy understand c++ solution


  • -1
    A
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            int res = 0, i = s.size() - 1;
            while(s[i] == ' ') i--;
            while(i >= 0 && s[i--] != ' ') res++;
            return res;
        }
    };

Log in to reply
 

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