class Solution {
public String mergeAlternately(String word1, String word2) {
String result = "";
int end = word1.length() > word2.length() ? word1.length() : word2.length();
for (int i = 0; i < end; i++) {
if (i < word1.length() && i < word2.length()) {
result += word1.charAt(i);
result += word2.charAt(i);
continue;
}
if (i < word1.length()) {
String record = word1.substring(i, word1.length());
result += record;
break;
}
if (i < word2.length()) {
String record = word2.substring(i, word2.length());
result += record;
break;
}
}
return result;
}
}
leetcode 1768. 交替合并字符串
最新推荐文章于 2021-05-07 11:28:48 发布