add: 221023-CN
This commit is contained in:
parent
ab8c056b6d
commit
075afd95c9
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue