This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

describe

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.

Example 1:

Input: nums = [1,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: Nums - ans = [nums [0], [1], the nums [2], nums [0], nums [1], the nums [2]] - ans =,2,1,1,2,1 [1]Copy the code

Example 2:

Input: nums = [1,3,2,1] Output: [1,3,2,1,1,3,2,1] Explanation: The array ans is formed as follows: Nums - ans = [nums [0], [1], the nums [2], nums [3], nums [0], nums [1], the nums [2], nums [3]] - ans =,3,2,1,1,3,2,1 [1]Copy the code

Note:

n == nums.length
1 <= n <= 1000
1 <= nums[i] <= 1000
Copy the code

parsing

Nums = nums = nums = nums = nums = nums = nums = nums = nums = nums

answer

class Solution(object):
    def getConcatenation(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        return nums+nums
Copy the code

answer

class Solution(object):
    def getConcatenation(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        return nums*2            	      
		
Copy the code

The results

Runtime: 10000 ms, the linked list for Concatenation of Array. 10000 MB, less than 100.00% of Python online submissions for Concatenation of ArrayCopy the code

parsing

The basic idea is to concatenate nums into nums, and then use the Python append function to append each nums element once after the nums list to get the answer.

answer

class Solution(object):
    def getConcatenation(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        for i in range(0,len(nums)):
            nums.append(nums[i])
        return nums
		
Copy the code

The results

Runtime: 10000 ms, faster than 27.80% of Python online submissions for Concatenation of Array. Submissions for Concatenation of ArrayCopy the code

parsing

I can’t think of a way to optimize it. I can only think of the most stupid method, which is to add the elements in NUMS to the result list in order, and then to the result in order. Do not recommend this method, have the suspicion of the word count I deeply think despise, but in order to make up the word count despise also can’t, fortunately thick skin. But look at the result as if this thought the slowest method is not the slowest, but the space is relatively large, hee hee.

answer

class Solution(object):
    def getConcatenation(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        result = []
        N = len(nums)
        for i in range(0, 2*N):
            result.append(nums[i%N])
        return result
		
Copy the code

The results

Runtime: 10000 ms, faster than 53.89% of Python online submissions for Concatenation of Array. Submissions for Concatenation of ArrayCopy the code

Link: leetcode.com/problems/co…

Your support is my greatest motivation