917.Reverse Only Letters
示例 1:
输入:"ab-cd"
输出:"dc-ba"
示例 2:
输入:"a-bC-dEf-ghIj"
输出:"j-Ih-gfE-dCba"
示例 3:
输入:"Test1ng-Leet=code-Q!"
输出:"Qedo1ct-eeLg=ntse-T!"
提示:
S.length <= 100
33 <= S[i].ASCIIcode <= 122
S 中不包含 \ or "Last updated
示例 1:
输入:"ab-cd"
输出:"dc-ba"
示例 2:
输入:"a-bC-dEf-ghIj"
输出:"j-Ih-gfE-dCba"
示例 3:
输入:"Test1ng-Leet=code-Q!"
输出:"Qedo1ct-eeLg=ntse-T!"
提示:
S.length <= 100
33 <= S[i].ASCIIcode <= 122
S 中不包含 \ or "Last updated
class Solution {
public:
string reverseOnlyLetters(string S) {
string alpha;
string res;
for(auto c:S)
if((c>='a' && c<='z' ) || (c>='A' && c<='Z') )
alpha+=c;
int start=0;
int len=alpha.length();
for(int i=0;i<S.length();i++)
{
if((S[i] >= 'a' && S[i]<='z') || (S[i]>='A' && S[i] <='Z'))
res+=alpha[len-1-start++];
else
res+=S[i];
}
return res;
}
};