LeetCode 58. Length of Last Word

题目

签到题

class Solution {
public:
    int lengthOfLastWord(string s) {
     
        int tag=0;
        int ans=0;
        
        for(int i=s.length()-1;i>=0;i--)
        {
            if(s[i]==' '&&tag==0)
                continue;
            if(s[i]==' '&&tag==1)
            {
                break;
            }
            if(s[i]!=' ')
            {
                tag=1;
                ans++;
            }
        }
        

            return ans;

    }
};
原文地址:https://www.cnblogs.com/dacc123/p/11474620.html