From 075afd95c950fda14abf840830aef0c85db49ec4 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sun, 23 Oct 2022 02:23:21 +0800 Subject: [PATCH] add: 221023-CN --- cpp/2210/221023-CN.cpp | 26 ++++++++++++++++++++++++++ cpp/2210/CMakeLists.txt | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 cpp/2210/221023-CN.cpp diff --git a/cpp/2210/221023-CN.cpp b/cpp/2210/221023-CN.cpp new file mode 100644 index 0000000..071c92f --- /dev/null +++ b/cpp/2210/221023-CN.cpp @@ -0,0 +1,26 @@ +#include + +/** + * 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; + } +}; \ No newline at end of file diff --git a/cpp/2210/CMakeLists.txt b/cpp/2210/CMakeLists.txt index 2aa7491..adbe255 100644 --- a/cpp/2210/CMakeLists.txt +++ b/cpp/2210/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2210) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2210 221022.cpp) +ADD_EXECUTABLE(2210 221023-CN.cpp)