Код: Выделить всё
class Solution {
public:
string mergeAlternately(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
string ans;
for (int i = 0, j = 0, k = 0; i < word1.length() + word2.length(); i++) {
if (i % 2 == 0 && j < len1 || k == len2) {
ans += word1[j];
j++;
}
if (i % 2 == 1 && k < len2 || j == len1) {
ans += word2[k];
k++;
}
}
return ans;
}
};
Подробнее здесь: https://stackoverflow.com/questions/783 ... tcode-1768
Мобильная версия