aboutsummaryrefslogtreecommitdiff
path: root/works/solutions/cpp/917.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-07-18 18:42:30 +0800
committercrupest <crupest@outlook.com>2020-07-18 18:42:30 +0800
commit9e2547bb9e7edf17d3235f94d4c7250c7a51c734 (patch)
tree37f3e0b21b99b1731dcb7f1f85500564241993c9 /works/solutions/cpp/917.cpp
parentbe7e712cd8605f045fabba10c53dbb46c186ec58 (diff)
downloadcrupest-9e2547bb9e7edf17d3235f94d4c7250c7a51c734.tar.gz
crupest-9e2547bb9e7edf17d3235f94d4c7250c7a51c734.tar.bz2
crupest-9e2547bb9e7edf17d3235f94d4c7250c7a51c734.zip
import(solutions): Add problem 917 .
Diffstat (limited to 'works/solutions/cpp/917.cpp')
-rw-r--r--works/solutions/cpp/917.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/works/solutions/cpp/917.cpp b/works/solutions/cpp/917.cpp
new file mode 100644
index 0000000..d01d795
--- /dev/null
+++ b/works/solutions/cpp/917.cpp
@@ -0,0 +1,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;
+ }
+};