1470. Shuffle the Array
生活随笔
收集整理的這篇文章主要介紹了
1470. Shuffle the Array
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給你一個數組 nums ,數組中有 2n 個元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。
請你將數組按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的數組。
示例 1:
輸入:nums = [2,5,1,3,4,7], n = 3 輸出:[2,3,5,4,1,7] 解釋:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案為 [2,3,5,4,1,7]示例 2:
輸入:nums = [1,2,3,4,4,3,2,1], n = 4 輸出:[1,4,2,3,3,2,4,1]示例 3:
輸入:nums = [1,1,2,2], n = 2 輸出:[1,2,1,2]
提示:
- 1 <= n <= 500
- nums.length == 2n
- 1 <= nums[i] <= 10^3
迭代
這題沒啥難度,直接迭代,一次按照索引把兩個數放入到列表中。
Python
class Solution:def shuffle(self, nums: List[int], n: int) -> List[int]:tmp = []for i in range(n):tmp.append(nums[i])tmp.append(nums[n + i])return tmp總結
以上是生活随笔為你收集整理的1470. Shuffle the Array的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1640. Check Array Fo
- 下一篇: 127. Word Ladder 单词接