blob: d01d7954dafec3200073c6532e5f939a95d1d2e8 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 | #include <string>
#include <cctype>
#include <utility>
using std::string;
class Solution
{
public:
    string reverseOnlyLetters(string s)
    {
        if (s.empty())
            return s;
        auto front = s.rend();
        auto back = s.end();
        bool move_front = true;
        while (true)
        {
            if (move_front)
            {
                if (std::isalpha(*--front))
                {
                    move_front = false;
                }
            }
            else
            {
                if (std::isalpha(*--back))
                {
                    std::swap(*front, *back);
                    move_front = true;
                }
            }
            if (front.base() == back)
            {
                break;
            }
        }
        return s;
    }
};
 |