Tarkibga o'tish

1929 - Concatenation of Array

πŸ‡¬πŸ‡§ Inglizcha: 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.

πŸ‡ΊπŸ‡Ώ O'zbekcha: n uzunlikga ega butun sonli array berilgan, siz ans 2n uzunlikga ega array yaratishingiz kerak qaysiki ans[i] == nums[i] va ans[i + n] == nums[i] ni qanoatlantirishi kerak 0 <= i < n.

ans bu ikkita nums ni birikmasi.

ans arrayni natija sifatida qaytaring.

🟒 Misol 1

$ 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]

🟒 Misol 2

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

πŸ”΄ Cheklovlar

  • n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000

Bizga nums ni uzunligi kerak bunga biz n o'zgaruvchisini yaratib nums ni uzunligini tenglab olamiz va 0 dan n gacha sikl ochib nums ga nums[i] qo'shib boramiz. Oxirida nums ni qaytarib yuboramiz.

⏰ Time Complexity: O(n)
πŸ“¦ Space Complexity: O(2n)

# Author: Abdulaminkhon Khaydarov
# Date: 02/11/22 
# Problem URL: https://leetcode.com/problems/concatenation-of-array/

from typing import List


class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        n = len(nums)
        for i in range(n):
            nums.append(nums[i])
        return nums


if __name__ == '__main__':
    solution = Solution()

    # Example 1
    print(solution.getConcatenation([1, 2, 1]) == [1, 2, 1, 1, 2, 1])

    # Example 2
    print(solution.getConcatenation([1, 3, 2, 1]) == [1, 3, 2, 1, 1, 3, 2, 1])

Pythonda list ni butun songa ko'paytirsa bo'ladi. Biz bu holatda nums ni 2 ga ko'paytirib natija sifatida qaytarib yuborsak bo'ldi.

⏰ Time Complexity: O(n)
πŸ“¦ Space Complexity: O(2n)

# Author: Abdulaminkhon Khaydarov
# Date: 02/11/22 
# Problem URL: https://leetcode.com/problems/concatenation-of-array/

from typing import List


class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        return nums * 2


if __name__ == '__main__':
    solution = Solution()

    # Example 1
    print(solution.getConcatenation([1, 2, 1]) == [1, 2, 1, 1, 2, 1])

    # Example 2
    print(solution.getConcatenation([1, 3, 2, 1]) == [1, 3, 2, 1, 1, 3, 2, 1])

Pythonda list ni list ga qo'shsa bo'ladi. nums + nums ham bizga bir xil natija beradi.

⏰ Time Complexity: O(n)
πŸ“¦ Space Complexity: O(2n)

# Author: Abdulaminkhon Khaydarov
# Date: 02/11/22 
# Problem URL: https://leetcode.com/problems/concatenation-of-array/

from typing import List


class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        return nums + nums


if __name__ == '__main__':
    solution = Solution()

    # Example 1
    print(solution.getConcatenation([1, 2, 1]) == [1, 2, 1, 1, 2, 1])

    # Example 2
    print(solution.getConcatenation([1, 3, 2, 1]) == [1, 3, 2, 1, 1, 3, 2, 1])

Oxirgi yangilanish: November 13, 2022 09:15:23