Leetcode 1929 Python, Concatenation of Array

Published at Oct 1, 2023

Problem

Given an integer array nums of length n, you want to create an array ans of length 2n whereans[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.

Solution 1

def arrayConcatenation(nums):
    nums = [1,2,3]
    ans = nums + nums
    return ans

Solution 2

  • Take an array and append it to itself
class Solution:
    def arrayConcatenation(self, nums):
        ans =[]
        for i in range(2):
            for n in nums:
                ans.append(n)
        return ans

subscribe

Subscribe to my newsletter to get regular content I publish on my blog.

© 2024 Ronald Otieno. All Rights Reserved.