My accepted c++ solution


  • 0
    G
    class Solution {
    public:
        int lengthOfLastWord(const char *s) {
            int n = strlen(s);
            int res = 0, ret = 0;
            for(int i = n-1; i >=0; i--)
            {
                if(s[i] == ' ' && ret == 0)
                    continue;
                if(s[i] != ' ')
                {    
                    res++;
                    ret = 1;
                }
                if(s[i] == ' ' && ret == 1) 
                    break;
            }
            return res;
        }
    };

Log in to reply
 

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