1. 程式人生 > >LeetCode Day46 length-of-last-word

LeetCode Day46 length-of-last-word

去掉末尾的空格,計算最後一個單詞的長度

class Solution {
public:
    int lengthOfLastWord(string s) {
        if(s.empty()) return {};
        int n=s.size()-1;
        for(;s[n]==' ';n--);
        cout<<n;
        for(int i=n;i>=0;i--){
            if(s[i]==' ') return (n-i);
        }
        return n+
1; } };