跳至主要內容

1768_交替合并字符串

T4mako算法数组小于 1 分钟

1768_交替合并字符串

简单

题目描述open in new window

class Solution {
    public String mergeAlternately(String word1, String word2) {
        StringBuilder res = new StringBuilder();
        int len = Math.min(word1.length(), word2.length());
        for (int i = 0; i < len; i++) {
            res.append(word1.charAt(i));
            res.append(word2.charAt(i));
        }
        return res.append(len == word1.length() ? word2.substring(len):word1.substring(len)).toString();    
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5