Leetcode-917 Reverse Only Letters(仅仅反转字母)

 1 class Solution
 2 {
 3     public:
 4         string reverseOnlyLetters(string S)
 5         {
 6             string onlyLetter;
 7             for(auto c:S)
 8             {
 9                 if(isalpha(c))
10                     onlyLetter += c;
11             }
12             reverse(onlyLetter.begin(),onlyLetter.end());
13             for(int i = 0,j = 0;i < S.size();i ++)
14             {
15                 if(isalpha(S[i]))
16                 {
17                     S[i] = onlyLetter[j++];
18                 }
19             }
20             return S;
21         }
22 };

 

posted @ 2018-10-09 19:20  Asurudo  阅读(174)  评论(0编辑  收藏  举报