add: 221023-CN

This commit is contained in:
Eatswap 2022-10-23 02:23:21 +08:00
parent ab8c056b6d
commit 075afd95c9
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 27 additions and 1 deletions

26
cpp/2210/221023-CN.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <string>
/**
* 1768. Merge Strings Alternately
*
* You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
* Return the merged string.
*/
class Solution {
public:
static std::string mergeAlternately(const std::string& s, const std::string& t) {
const char* sp = s.c_str(), * tp = t.c_str();
std::string ret;
ret.reserve(s.length() + t.length());
while (*sp && *tp) {
ret.push_back(*(sp++));
ret.push_back(*(tp++));
}
while (*sp)
ret.push_back(*(sp++));
while (*tp)
ret.push_back(*(tp++));
return ret;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2210)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2210 221022.cpp) ADD_EXECUTABLE(2210 221023-CN.cpp)