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
相关推荐
仪器科学与传感技术博士15 分钟前
Matplotlib库:Python数据可视化的基石,发现它的美
开发语言·人工智能·python·算法·信息可视化·matplotlib·图表可视化
啾啾Fun36 分钟前
PyTorch 核心三件套:Tensor、Module、Autograd
人工智能·pytorch·python
success2 小时前
【爆刷力扣-二叉树】层次遍历
算法
嫩萝卜头儿3 小时前
深入理解 Java AWT Container:原理、实战与性能优化
java·python·性能优化
爱吃芒果的蘑菇3 小时前
使用pybind11封装C++API
开发语言·c++·python
2501_924880703 小时前
手机拍照识别中模糊场景准确率↑37%:陌讯动态适配算法实战解析
人工智能·深度学习·算法·计算机视觉·智能手机·视觉检测
雲_kumo3 小时前
正则表达式
python·正则表达式
Smile_2542204183 小时前
散点图矩阵
python
BUG再也不见4 小时前
Python爬虫 urllib 模块详细教程:零基础小白的入门指南
开发语言·网络·爬虫·python
lifallen4 小时前
HBase的异步WAL性能优化:RingBuffer的奥秘
大数据·数据库·分布式·算法·性能优化·apache·hbase