-
[LeetCode] 1929. Concatenation of Array c++Coding Test/LeetCode 2022. 10. 10. 13:10728x90
- 문제
Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).
Specifically, ans is the concatenation of two nums arrays.
Return the array ans.
Input: nums = [1,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] - ans = [1,2,1,1,2,1]
- 문제 해결
class Solution { public: vector<int> getConcatenation(vector<int>& nums) { vector<int> answer(nums); for(const auto &it : nums){ answer.push_back(it); } nums.assign(answer.begin(), answer.end()); return answer; } };
다른 풀이
class Solution { public: vector<int> getConcatenation(vector<int>& nums) { int n=nums.size(); for(int i=0;i<n;i++) { nums.push_back(nums[i]); } return nums; } };
728x90'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 35. Search Insert Position c++ (0) 2022.10.10 [LeetCode] 1480. Running Sum of 1d Array c++ (0) 2022.10.10 [LeetCode] 897. Increasing Order Search Tree c++ (0) 2022.09.27 [LeetCode] 617. Merge Two Binary Trees c++ (0) 2022.09.27 [LeetCode] 2331. Evaluate Boolean Binary Tree c++ (0) 2022.09.26