Leetcode 1768. Merge Strings Alternately

Problem

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.

Return the merged string...

Algorithm

Insert numbers alternately

Code

python3 复制代码
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        ans = ""
        L1, L2, P1, P2 = len(word1), len(word2), 0, 0
        while P1 < L1 or P2 < L2:
            if P1 < L1:
                ans += word1[P1]
                P1 += 1
            if P2 < L2:
                ans += word2[P2]
                P2 += 1
        return ans
相关推荐
剑穗挂着新流苏312几秒前
208_深度学习的鲁棒性之美:暂退法(Dropout)原理与实战
开发语言·pytorch·python·深度学习
SeatuneWrite5 分钟前
AI仿真人剧供应商2025推荐,高效内容创作与分发解决方案
人工智能·python
ATMQuant11 分钟前
以AI量化为生:20.实时图表交易系统开发
python·量化交易·实盘交易·vnpy·k线图表
A~MasterYi14 分钟前
深入理解 Microscaling (MX) 格式:从浮点基础到共享指数矩阵乘法
算法·矩阵
搜狐技术产品小编202318 分钟前
端侧Python动态算法策略的部署与运行
开发语言·python
时光书签34 分钟前
了解脚本语言
python·bash·batch命令
ID_1800790547338 分钟前
Python 采集转转二手商品详情:API 接口与爬虫实战全方案(2026 最新)
开发语言·爬虫·python
环黄金线HHJX.41 分钟前
《Tuan(拼音字母)⇆团(Group)/&湍(Turbulence)/&双结构链路道/&文字、符号、语言/&源点设计、连接起:人类与自然+AICosmOS》
开发语言·人工智能·算法·编辑器
有时间要学习1 小时前
面试150——第七周
算法·面试·深度优先
m0_747124531 小时前
LangChain 索引增强对话链详解
python·ai·langchain