while A and B: 同时满足A和B条件
while A or B:满足A或B任意一条
以1768. 交替合并字符串官方答案为启发,以前没意识到while还可以用and和or。
其实无论用什么,只要看最后的结果是True或False
class Solution: def mergeAlternately(self, word1: str, word2: str) -> str: m, n = len(word1), len(word2) i = j = 0 ans = list() while i < m or j < n: if i < m: ans.append(word1[i]) i += 1 if j < n: ans.append(word2[j]) j += 1 return "".join(ans) print(Solution().mergeAlternately(word1="abcd", word2="pq"))
标签:Python,word2,while,word1,str,ans From: https://www.cnblogs.com/daizichuan/p/17671449.html