simple c++ solution


  • 1
    D
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            if(s.empty()) return 0;
            int n = s.length();
            while(s[n-1] == ' ') n--;
            int i = n -1;
            while(i>=0 && s[i] != ' ') i--;
            
            return n-1 -i;
            
        }
    };
    

Log in to reply
 

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