题解 | 单词倒排

单词倒排

https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

#include <iostream>
#include <string>
#include<vector>
#include <stack>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    stack<string> str_2;
    string  word = "";
    for (char c : str) {
        if ( ( c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z' )) {
            word.push_back(c);
        } else {
            if (!word.empty()) {
                str_2.push(word);
                word = "";
            }

        }
    }
    if (!word.empty()) {
        str_2.push(word);
    }
    while (!str_2.empty()) {
        cout << str_2.top() << " " ;
        str_2.pop();
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

有点小bug,for循环条件不完善,最后一个字符是字母的时候不会触发压栈,所以偷懒额外增加了了一次压栈。

全部评论

相关推荐

11-11 16:40
已编辑
门头沟学院 人工智能
不知道怎么取名字_:这个有点不合理了,相当于已经毕业了,但还是没转正,这不就是白嫖
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务