class Solution {
public String mergeAlternately(String word1, String word2) {
int len1 = word1.length(), len2 = word2.length();
int index = 0;
char[] result = new char[len1 + len2];
for(int i = 0; i < len1 || i < len2; i++){
if(i < len1) result[index++] = word1.charAt(i);
if(i < len2) result[index++] = word2.charAt(i);
}
return new String(result);
}
}
1768.交替合并字符串
最新推荐文章于 2024-11-01 15:19:21 发布