My simple C++ solution


  • 0
    E
    class Solution {
    	public:
        int lengthOfLastWord(string s) {
            if(s.empty()){return 0;}
            int i = s.length()-1;
            int cnt = 0;
            while(s[i]==' '){--i;}//找到第一个非空格
            while(i>=0)
            {
                if(isalpha(s[i--])){++cnt;}
                else{break;}
            }
            return cnt;
        }
    	};
    

Log in to reply
 

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