leetcode - 917. 仅仅反转字母
题目
代码
利用双指针:
#include <iostream>
#include <string>
using namespace std;
string reverseOnlyLetters(string s) {
int n = s.size();
int left = 0, right = n-1;
char ch;
while(left < right){
if(isalpha(s[left]) && isalpha(s[right])){
ch = s[left];
s[left] = s[right];
s[right] = ch;
left++;
right--;
}else if(!isalpha(s[left])){
left++;
}else{
right--;
}
}
return s;
}
int main(){
string s, res;
cin>>s;
res = reverseOnlyLetters(s);
cout<<res;
return 0;
}