add: 220223-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-02-23 23:27:38 +08:00
parent ecb3d135df
commit 6e2b99ed98
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 26 additions and 1 deletions

25
cpp/2202/220223-CN.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <string>
#include <cctype>
#include <iostream>
class Solution {
public:
static std::string reverseOnlyLetters(std::string s) {
int L = 0, R = s.length() - 1;
for (; L < R && !std::isalpha(s[L]); ++L);
for (; R && !std::isalpha(s[R]); --R);
for (; L < R; ++L, --R) {
for (; L < R && !std::isalpha(s[L]); ++L);
for (; R && !std::isalpha(s[R]); --R);
if (L >= R)
break;
std::swap(s[L], s[R]);
}
return s;
}
};
int main() {
std::cout << Solution::reverseOnlyLetters("-");
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220222.cpp)
ADD_EXECUTABLE(2202 220223-CN.cpp)