在朋友推荐下刷了力扣。
第一题,间隔合并文字,折磨了我好久。
一开始思路就想了很久很久。
少用了个列表和.join()省了点内存,但卡在17.3m下不去。
直接用生成器反而让内存占用更多。
发不出来图,码是这样的:
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
i=0
abc=""
while i<len(word1) or i< len(word2):
if i< len(word1):
abc+=word1[i]
if i< len(word2):
abc+=word2[i]
i+=1
return abc
空间复杂度排在前3%,不知道剩下2%用了什么方法。