description:

找到一句话中最后一个单词的长度
Note:

Example:

Example:

Input: "Hello World"
Output: 5

answer:

class Solution {
public:
    int lengthOfLastWord(string s) {
        if (s.empty()) return 0;
        int l = s.size();
        int left = 0,right = l -1;
        while(s[left] == ' ') ++left; // 去空格
        while(s[right] == ' ') -- right; // 去空格
        if (left == right) return 1;
        int res = 0;
        for (int i = right; i >= left; --i) {
            if (s[i] == ' ') {
                return res;
            } else {
                res += 1;
            }
        }
        return res;
    }
};

relative point get√:

hint :

先把句子开头和结尾的空格去了