leetcode 58. Length of Last Word

这道题主要要注意string 末尾有很多空格的情况,C++ 里面空格的判断是用单引号,顺便学习一下revese 需要#include
leetcode 58. Length of Last Word



#include<algorithm>
class Solution {
public:
    int lengthOfLastWord(string s) {
    	
    	
    	int num = s.size();
		if (num==0)
		return 0;
		//reverse(s.begin(),s.end()); 
		int i=0;
		int count=0;
		for ( i= num-1 ; i>=0 ; i--)
		{
			if ( s[i] != ' '   )
			 count ++;
            else
                if( s[i] == ' ' && count == 0)
                    continue;
            else
                break;
		}
	    
		return count;
        
    }
};