Posted on 2023-04-18 17:28
Uriel 阅读(28)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
将两个字符串交替merge成一个,水题
1 #1768
2 #Runtime: 19 ms (Beats 58.68%)
3 #Memory: 13.6 MB (Beats 11.81%)
4
5 class Solution(object):
6 def mergeAlternately(self, word1, word2):
7 """
8 :type word1: str
9 :type word2: str
10 :rtype: str
11 """
12 ans = []
13 for i in range(min(len(word1), len(word2))):
14 ans.append(word1[i])
15 ans.append(word2[i])
16 if i < len(word1):
17 ans.append(word1[i + 1:])
18 if i < len(word2):
19 ans.append(word2[i + 1:])
20 return ''.join(ans)