TreeviewCopyright © aleen42 all right reserved, powered by aleen42
1470. 重新排列数组
Java
/*
* @Author: Goog Tech
* @Date: 2020-08-15 21:57:50
* @LastEditTime: 2020-08-15 21:58:08
* @Description: https://leetcode-cn.com/problems/shuffle-the-array/
* @FilePath: \leetcode-googtech\#1470. Shuffle the Array\Solution.java
* @WebSite: https://algorithm.show/
*/
class Solution {
public int[] shuffle(int[] nums, int n) {
if(nums.length == 2) return nums;
int[] result = new int[n << 1];
for(int i = 0; i < n; i++) {
result[2 * i] = nums[i]; // 偶数
result[2 * i + 1] = nums[i + n]; // 奇数
}
return result;
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-15 21:57:56
LastEditTime: 2020-08-15 21:59:18
Description: https://leetcode-cn.com/problems/shuffle-the-array/
FilePath: \leetcode-googtech\#1470. Shuffle the Array\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def shuffle(self, nums, n):
"""
:type nums: List[int]
:type n: int
:rtype: List[int]
"""
nums1, nums2, nums3 = nums[:n], nums[n:], []
for i in range(n):
nums3.append(nums1[i])
nums3.append(nums2[i])
return nums3