首页 > 代码库 > Length of Last Word

Length of Last Word

方法:最简单的是从后向前遍历即可

class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = 0, i=s.size()-1;
        
        while(i>=0 && s[i] ==  )
            --i;
        while(i>=0 && s[i--] !=  )
            ++len;
         
         return len;
    }
};

 

Length of Last Word