# LeetCode - 0917. Reverse Only Letters ### 題目網址:https://leetcode.com/problems/reverse-only-letters/ ###### tags: `LeetCode` `Easy` `字串` ```cpp= /* -LeetCode format- Problem: 917. Reverse Only Letters Difficulty: Easy by Inversionpeter */ class Solution { public: string reverseOnlyLetters(string S) { int first = 0, second = S.size() - 1; while (1) { while (first != S.size() && !isalpha(S[first])) ++first; while (second >= 0 && !isalpha(S[second])) --second; if (first >= second) break; swap(S[first], S[second]); ++first; --second; } return S; } }; ```