示例 1:
输入:s = “ab-cd”
输出:“dc-ba”
示例 2:
输入:s = “a-bC-dEf-ghIj”
输出:“j-Ih-gfE-dCba”
示例 3:
输入:s = “Test1ng-Leet=code-Q!”
输出:“Qedo1ct-eeLg=ntse-T!”
提示
1 <&#61; s.length <&#61; 100
s 仅由 ASCII 值在范围 [33, 122] 的字符组成
s 不含 ‘"’ 或 ‘\’
程序代码
class Solution:def reverseOnlyLetters(self, s: str) -> str:ans &#61; list(s)i, j &#61; 0, len(ans) - 1while True:while i < j and ans[i].isalpha() &#61;&#61; False:i &#43;&#61; 1while i < j and ans[j].isalpha() &#61;&#61; False:j -&#61; 1if i < j:ans[i], ans[j] &#61; ans[j], ans[i]else:breaki &#43;&#61; 1j -&#61; 1return &#39;&#39;.join(ans)