#include <cctype>
#include <vector>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return string字符串
     */
    string reverseWords(string s) {
        // write code here
        vector<string>ve;
        int ls = s.size();
        string ss = "";
        int flag = 0;
        for (int i = 0; i < ls; ++i) {
            if (s[i] == ' ' && flag == 0)
                continue;
            if (s[i] == ' ' && s[i + 1] == ' ')
                continue;
            if (isalpha(s[i]))
                ss += s[i], flag = 1;
            else if (s[i] == ' ' && i != ls - 1) {
                ve.push_back(ss);
                ss = "";
            }
        }
        ve.push_back(ss);
        string cnt = "";
        int ans = ve.size();
        cnt += ve[ans - 1];
        for (int i = ans - 2; i >= 0; --i) {
            cnt += " ";
            cnt += ve[i];
        }
        return cnt;
    }
};

一、题目考察的知识点

字符串模拟

二、题目解答方法的文字分析

这题比较啰嗦,要考虑到各种情况,如果嫌麻烦可以直接使用字符流去做,否则就要不断地试错,比如开头就有空格,这时候就要特判,比如有连续空格也要特判,比如说尾部有空格,也要特判,解决这些题目就基本上解决了,具体看代码,这代码也是在无数wa声中才过的。

听取wa声一片~

三、本题解析所用的编程语言

c++