From d3fde905b1834e2349603e773db8e8388034e806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:28:24 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=201929=20(Concate?= =?UTF-8?q?nation=20of=20Array)=20from=20Swift=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/1929.swift with solutions/1901-2000/1929-concatenation-of-array/. --- .../1901-2000/1929-concatenation-of-array/README.md | 12 ++++++++++++ .../1929-concatenation-of-array/solution.cpp | 13 +++++++++++++ .../1929-concatenation-of-array/solution.hpp | 10 ++++++++++ .../1901-2000/1929-concatenation-of-array/test.cpp | 8 ++++++++ swift/1929.swift | 9 --------- 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 solutions/1901-2000/1929-concatenation-of-array/README.md create mode 100644 solutions/1901-2000/1929-concatenation-of-array/solution.cpp create mode 100644 solutions/1901-2000/1929-concatenation-of-array/solution.hpp create mode 100644 solutions/1901-2000/1929-concatenation-of-array/test.cpp delete mode 100644 swift/1929.swift diff --git a/solutions/1901-2000/1929-concatenation-of-array/README.md b/solutions/1901-2000/1929-concatenation-of-array/README.md new file mode 100644 index 0000000..77cd357 --- /dev/null +++ b/solutions/1901-2000/1929-concatenation-of-array/README.md @@ -0,0 +1,12 @@ +# 1929. Concatenation of Array + +[LeetCode problem 1929](https://leetcode.com/problems/concatenation-of-array/) + +- **Difficulty**: Easy +- **Tags**: array, simulation + +## Description + +Given an integer array `nums` of length `n`, return an array `ans` of length +`2n` where `ans[i] == nums[i]` and `ans[i + n] == nums[i]` for `0 <= i < n` +(i.e. `nums` concatenated with itself). diff --git a/solutions/1901-2000/1929-concatenation-of-array/solution.cpp b/solutions/1901-2000/1929-concatenation-of-array/solution.cpp new file mode 100644 index 0000000..f75db16 --- /dev/null +++ b/solutions/1901-2000/1929-concatenation-of-array/solution.cpp @@ -0,0 +1,13 @@ +#include "solution.hpp" + +namespace leetcode::p1929 { + +std::vector solve(const std::vector& nums) { + std::vector result; + result.reserve(nums.size() * 2); + result.insert(result.end(), nums.begin(), nums.end()); + result.insert(result.end(), nums.begin(), nums.end()); + return result; +} + +} // namespace leetcode::p1929 diff --git a/solutions/1901-2000/1929-concatenation-of-array/solution.hpp b/solutions/1901-2000/1929-concatenation-of-array/solution.hpp new file mode 100644 index 0000000..39e9a0f --- /dev/null +++ b/solutions/1901-2000/1929-concatenation-of-array/solution.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace leetcode::p1929 { + +// optimized: O(n) time, O(n) space — append nums to a copy of itself. +std::vector solve(const std::vector& nums); + +} // namespace leetcode::p1929 diff --git a/solutions/1901-2000/1929-concatenation-of-array/test.cpp b/solutions/1901-2000/1929-concatenation-of-array/test.cpp new file mode 100644 index 0000000..ba01b9b --- /dev/null +++ b/solutions/1901-2000/1929-concatenation-of-array/test.cpp @@ -0,0 +1,8 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 1929", "[p1929]") { + CHECK(leetcode::p1929::solve({1, 2, 1}) == std::vector{1, 2, 1, 1, 2, 1}); + CHECK(leetcode::p1929::solve({1, 3, 2, 1}) == std::vector{1, 3, 2, 1, 1, 3, 2, 1}); +} diff --git a/swift/1929.swift b/swift/1929.swift deleted file mode 100644 index 9eaf9b8..0000000 --- a/swift/1929.swift +++ /dev/null @@ -1,9 +0,0 @@ -func getConcatenation(_ nums: [Int]) -> [Int] { - return nums + nums -} - -let test1 = [1,2,1] -let test2 = [1,3,2,1] - -print(getConcatenation(test1)) // [1,2,1,1,2,1] -print(getConcatenation(test2)) // [1,3,2,1,1,3,2,1] \ No newline at end of file