diff --git a/CMakeLists.txt b/CMakeLists.txt index ea54e25f..a1ae4cae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,6 +94,7 @@ add_task(combination-sum-ii) add_task(combination-sum-iii) add_task(combination-sum-iv) add_task(combinations) +add_task(concatenation-of-array) add_task(construct-binary-tree-from-inorder-and-postorder-traversal) add_task(construct-binary-tree-from-preorder-and-inorder-traversal) add_task(construct-quad-tree) @@ -435,9 +436,9 @@ add_task(plus-one) add_task(populating-next-right-pointers-in-each-node) add_task(populating-next-right-pointers-in-each-node-ii) add_task(possible-bipartition) -add_task(powx-n) add_task(power-of-three) add_task(power-of-two) +add_task(powx-n) add_task(predict-the-winner) add_task(product-of-array-except-self) add_task(product-of-the-last-k-numbers) diff --git a/solutions/concatenation-of-array/CMakeLists.txt b/solutions/concatenation-of-array/CMakeLists.txt new file mode 100644 index 00000000..ee0312df --- /dev/null +++ b/solutions/concatenation-of-array/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_concatenation_of_array test.cpp) diff --git a/solutions/concatenation-of-array/solution.hpp b/solutions/concatenation-of-array/solution.hpp new file mode 100644 index 00000000..4e0dbd04 --- /dev/null +++ b/solutions/concatenation-of-array/solution.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +class Solution { +public: + static std::vector getConcatenation(const std::vector &nums) { + const auto n = nums.size(); + + std::vector res(2 * n); + for (size_t i = 0; i < n; ++i) { + res[i] = res[i + n] = nums[i]; + } + + return res; + } +}; diff --git a/solutions/concatenation-of-array/test.cpp b/solutions/concatenation-of-array/test.cpp new file mode 100644 index 00000000..55d2461e --- /dev/null +++ b/solutions/concatenation-of-array/test.cpp @@ -0,0 +1,16 @@ +#include + +#include + +TEST_CASE("Simple") { + { + std::vector nums{1, 2, 1}; + std::vector expected{1, 2, 1, 1, 2, 1}; + REQUIRE(expected == Solution::getConcatenation(nums)); + } + { + std::vector nums{1, 3, 2, 1}; + std::vector expected{1, 3, 2, 1, 1, 3, 2, 1}; + REQUIRE(expected == Solution::getConcatenation(nums)); + } +}