思路
获取两个数组中较短长度的数组长度,然后按照较短长度数组分别压入两个数组相应的元素。再将较长数组的剩余元素压入string即可。
class Solution {
public String mergeAlternately(String word1, String word2) {
char[] a = new char[word1.length()+word2.length()];
int i=0,j=0;
int min = word1.length()>=word2.length()?word2.length():word1.length();
for(i=0,j=0;i<min*2;j++){
if(j<min){
a[i++]=word1.charAt(j);
a[i++]=word2.charAt(j);}
}
while(j<word1.length()){
a[i++]=word1.charAt(j);
j++;
}
while(j<word2.length()){
a[i++]=word2.charAt(j);
j++;
}
return new String(a);
}
}