算法養成記:Reverse Only Letters
- 2020 年 3 月 10 日
- 筆記
呆萌程序員
算法養成記







LeetCode917
Reverse Only Letters
Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.
Example 1:
Input: "ab-cd"
Output: "dc-ba"
Example 2:
Input: "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
Example 3:
Input: "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
Note:
1.S.length <= 100
2.33 <= S[i].ASCIIcode <= 122
3.S doesn't contain or "


























在實際測試里,三種算法的結果都是遞增的
執行用時分別是:2ms,1ms,1ms
內存消耗分別是:37.1MB,37.5MB,37MB
循環兩次的用時不一定比一次的少;
還有個小插曲,在寫方法1的時候,也寫過用Stack,畢竟首先想到它。但他的內存消耗竟然比LinkedList還高,達到37.6MB,耗時還是2ms,哭瞎!
哪裡不太對,但又說不上來。
數據結構和算法一直都是程序員面試重點。寫好每一個方法,每一個接口,程序的效率也會越來越高。為了學習和鞏固數據結構和算法,我們特別創作了《呆萌程序員–明明凱凱算法養成記》,每天更新一篇數據結構知識點或者刷一道LeetCode題目。算法都會在LeetCode上測試。當然答案不唯一,由於能力有限,實現方法不一定是最好的,也希望各位小夥伴一起來學習分享~