EasyNo. 1470
Shuffle the Array — LeetCode 1470 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn].
Example
- Input
- nums = [2,5,1,3,4,7], n = 3
- Output
- [2,3,5,4,1,7]
- Explanation
- Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
Python solution
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
return [x for pair in zip(nums[:n], nums[n:]) for x in pair]Complexity
- Time
- O(n)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 1470. Shuffle the Array?
LeetCode 1470. Shuffle the Array is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1470. Shuffle the Array?
The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1470. Shuffle the Array?
The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 1470. Shuffle the Array cover?
LeetCode 1470. Shuffle the Array is tagged Array on LeetCode.