TGViewer
Python | LeetCode Python | LeetCode @easy_python_task · 9.04K subscribers
Post #2375 679
Задача: 1470. Shuffle the Array
Сложность: easy

Дан массив nums, состоящий из 2n элементов в форме [x1, x2, ..., xn, y1, y2, ..., yn].

Верните массив в форме [x1, y1, x2, y2, ..., xn, yn].

Пример:
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].


👨‍💻 Алгоритм:

1⃣Создайте массив result размером 2 * n.

2⃣Итеративно пройдите по массиву nums от 0 до n - 1:
Сохраните элемент xi+1, то есть nums[i], в индекс 2 * i массива result.
Сохраните элемент yi+1, то есть nums[i + n], в индекс 2 * i + 1 массива result.

3⃣Верните массив result.

😎 Решение:
class Solution:
def shuffle(self, nums, n):
result = [0] * (2 * n)
for i in range(n):
result[2 * i] = nums[i]
result[2 * i + 1] = nums[n + i]
return result


Ставь 👍 и забирай 📚 Базу знаний
More from @easy_python_task
  1. Oct 10, 2026Post #2435
  2. Oct 10, 2026Задача: 1249. Minimum Remove to Make Valid Parentheses Сложность: medium Дана строка s из…
  3. Oct 9, 2026Post #2433
  4. Oct 7, 2026🔥 Скрытые вакансии с удаленной работой для Python разработчика, которые нигде больше не п…
  5. Oct 4, 2026Задача: 958. Check Completeness of a Binary Tree Сложность: medium Дан корень бинарного де…
  6. Oct 4, 2026Задача: 949. Largest Time for Given Digits Сложность: medium Учитывая массив arr из 4 цифр…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →