leetcode+ 最后一个单词长度,用栈,有点类似于反转一串单词输出

点击打开链接
class Solution {
public:
    int lengthOfLastWord(string s) {
        if(s.size()==0) return 0;
        int len = s.size(),num=0;
        while(s[len-1-num]==' ') num+=1;
        stack<char> st;
        for(int i=s.size()-1-num; i>=0; i--){
            if(s[i]==' ') break;
            st.push(s[i]);
        }
        return st.size();
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/80781513
今日推荐