Daily LeetCode – day0086 1768. Merge Strings Alternately

// 1768. Merge Strings Alternately
class Solution {
    public String mergeAlternately(String word1, String word2) {
        StringBuilder sb = new StringBuilder();
        int length1 = word1.length();
        int length2 = word2.length();
        if (length1 < length2) {
            for (int i = 0; i < length1; ++i) {
                sb.append(word1.charAt(i));
                sb.append(word2.charAt(i));
            }
            sb.append(word2.substring(length1));
        } else {
            for (int i = 0; i < length2; ++i) {
                sb.append(word1.charAt(i));
                sb.append(word2.charAt(i));
            }
            sb.append(word1.substring(length2));
        }
        return sb.toString();
    }
}
学习笔记:
这是一道字符串的简单题,就是合并字符串,多的接上去就行了。


关于樊轶群

一个善良的理想主义者。
此条目发表在每日LeetCode分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注