题解 | 单词倒排
单词倒排
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循环条件不完善,最后一个字符是字母的时候不会触发压栈,所以偷懒额外增加了了一次压栈。
顺丰集团工作强度 372人发布