LeetCode #1470. Shuffle the Array
题目
解题方法
设置一个新列表rat和两个指针x、y,x从0开始,y从n开始,每次按(x,y)数对写入rat中即可。
时间复杂度:O(n)
空间复杂度:O(n)
代码
class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:
        rat = []
        x = 0
        y = n
        
        while x != n:
            rat.append(nums[x])
            rat.append(nums[y])
            x += 1
            y += 1
        
        return rat

                
            
        
浙公网安备 33010602011771号