C++ solution


  • 0
    I
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            if(s.length() == 0) return 0;
            int count = 0;
            int pos = s.length() - 1;
            while(pos >= 0) {
                if(s[pos] == ' ' && count != 0) {
                    break;
                }
                if(s[pos] != ' ') count++;
                pos--;
            }
            return count;
        }
    };
    

Log in to reply
 

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