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