代码1
class Solution {
public:
string mergeAlternately(string w1, string w2) {
string s;
int n = w1.size(), m = w2.size(), i = 0, j = 0;
while(i<n && j<m){
s+=w1[i++];
s+=w2[j++];
}
while(i<n) s+=w1[i++];
while(j<m) s+=w2[j++];
return s;
}
};
代码2
class Solution {
public:
string mergeAlternately(string a, string b) {
string res;
int i = 0, j = 0;
while (i < a.size() || j < b.size()) {
if (i < a.size()) res += a[i ++ ];
if (j < b.size()) res += b[j ++ ];
}
return res;
}
};