add: 230418

This commit is contained in:
Eatswap 2023-04-19 01:14:40 +08:00
parent 2e711e443b
commit 6b5ae005d9
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 36 additions and 1 deletions

35
cpp/2304/230418.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <algorithm>
#include <iterator>
#include <ranges>
#include <string>
#include <iostream>
/**
* 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 {
using S = std::string;
using CSR = S const&;
public:
static S mergeAlternately(CSR, CSR);
};
std::string Solution::mergeAlternately(CSR s, CSR t) {
const int m = s.length(), n = t.length(), k = std::min(m, n);
std::string ret;
for (int i = 0; i < k; ++i)
ret.push_back(s[i]), ret.push_back(t[i]);
for (int i = k; i < m; ++i)
ret.push_back(s[i]);
for (int i = k; i < n; ++i)
ret.push_back(t[i]);
return ret;
}
int main() {
std::cout << Solution::mergeAlternately("ab", "pqrs");
}

View File

@ -4,4 +4,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true) SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2304 230417-CN.cpp) ADD_EXECUTABLE(2304 230418.cpp)