水题,注意vector和isalpha的使用。
c++代码如下:
class Solution {
public:
string reverseOnlyLetters(string S) {
string s(S);
int len = S.length();
int j=len-1;
for(int i=0; i<len; i++){
if(isalpha(s[i])){
for(; j>=0; j--){
if(isalpha(S[j])){
break;
}
}
s[i]=S[j];
j--;
}
}
return s;
}
};