十七
十七
Published on 2022-02-23 / 170 Visits
0
0

leetcdoe58. 最后一个单词的长度

leetcdoe58. 最后一个单词的长度

class Solution {
public:
    int lengthOfLastWord(string s) {
            int ans=0,cur=s.length()-1;
            while(s[cur]==' '){
                    cur--;
            }
            while(cur>=0&&s[cur]!=' '){
                ans++;
                cur--;
            }
        return ans;
    }
};

Comment