Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
将两个字符串交替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)


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理