【LeetCode058】Length of Last Word

class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = s.length();
        int result = 0;
        for(int i = len-1; i >= 0; --i){
            if(s[i] == ' '){
                if(result == 0)
                    continue;  //加上这一句才更符合题意
                else
                    return result;
            }else
                ++result;
        }
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_39458342/article/details/87690395