Skip to content

Commit

Permalink
Concatenation of Array
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 1, 2023
1 parent 438ca00 commit e872c85
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions solutions/concatenation-of-array/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_concatenation_of_array test.cpp)
18 changes: 18 additions & 0 deletions solutions/concatenation-of-array/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <algorithm>
#include <vector>

class Solution {
public:
static std::vector<int> getConcatenation(const std::vector<int> &nums) {
const auto n = nums.size();

std::vector<int> res(2 * n);
for (size_t i = 0; i < n; ++i) {
res[i] = res[i + n] = nums[i];
}

return res;
}
};
16 changes: 16 additions & 0 deletions solutions/concatenation-of-array/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
{
std::vector<int> nums{1, 2, 1};
std::vector<int> expected{1, 2, 1, 1, 2, 1};
REQUIRE(expected == Solution::getConcatenation(nums));
}
{
std::vector<int> nums{1, 3, 2, 1};
std::vector<int> expected{1, 3, 2, 1, 1, 3, 2, 1};
REQUIRE(expected == Solution::getConcatenation(nums));
}
}

0 comments on commit e872c85

Please sign in to comment.