1768. 交替合并字符串
给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。
示例 1:
- 输入:word1 = "abc", word2 = "pqr"
- 输出:"apbqcr"
- 解释:字符串合并情况如下所示:
- word1: a b c
- word2: p q r
- 合并后: a p b q c r
class Solution {
public:
string mergeAlternately(string word1, string word2) {
string res="";
int i=0,j=0;
while(i<word1.size()&&j<word2.size()){
res+=word1[i++];
res+=word2[j++];
}
while(i<word1.size())
res+=word1[i++];
while(j<word2.size())
res+=word2[j++];
return res;
}
};
标签:string,合并,交替,1768,word1,word2,字符串
From: https://www.cnblogs.com/SkyDusty/p/16818056.html