LeetCode 20200127 (the length of the last word)

1. The length of the last word
Note 1. If the end of the string is to consider how to do so with the length- spaces will need to consider the length reduction
Note 2 and then stored word length of this parameter to zero once encountered a space with a variable otherwise be count
Note 3 continue

class Solution {
public:
    int lengthOfLastWord(string s) {
        int len=s.size();
        int res=0;
        while(s[len-1]==' '){
            len--;
        }
        for(int i=0;i<len;i++){
            if(s[i]==' '){
                res=0;
                continue;
            }
            res++;
        }
        return res;
    }
};
Published 44 original articles · won praise 9 · views 3340

Guess you like

Origin blog.csdn.net/puying1/article/details/104105348