非常感謝你閱讀本文~
歡迎【👍點贊】【?收藏】【📝評論】~
放棄不難,但堅持一定很酷~
希望我們大家都能每天進步一點點~
本文由 二當家的白帽子 https://le-yi.blog.csdn.net/ 博客原創~
文章目錄
- 1470. 重新排列陣列:
- 樣例 1
- 樣例 2
- 樣例 3
- 提示
- 分析
- 題解
- java
- c
- c++
- python
- go
- rust
- 原題傳送門:https://leetcode-cn.com/problems/shuffle-the-array/
1470. 重新排列陣列:
給你一個陣列 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
分析
- 這道演算法題是簡單題,所以又可以重拳出擊了,
- 回傳的結果不算在空間復雜度上,所以直接開辟新的空間賦值回傳結果就可以了,
- 但是二當家的發現可以難為自己,于是又靈機一動,原地排列,我真是個小機靈鬼,
- 想要原地排列就一定要存盤中間結果,原來的位置被占以后,需要知道原來位置的數字,
- 提示里說每個數字最大不超過1000,而且都是正數,所以只需要用10個二進制位就夠了,
- 引數傳遞進來的陣列一般都是int型陣列,int型一般都是32位,夠存題目中的2個數了,
題解
java
class Solution {
public int[] shuffle(int[] nums, int n) {
// 申請新的陣列,這樣用空間換時間
int[] ans = new int[n * 2];
for (int i = 0; i < n; ++i) {
ans[2 * i] = nums[i];
ans[2 * i + 1] = nums[n + i];
}
return ans;
}
}
class Solution {
public int[] shuffle(int[] nums, int n) {
// 原地排列,時間換空間
for (int i = 0; i < n; ++i) {
// 把重排列的值左移10位
nums[2 * i] |= (nums[i] & 0b1111111111) << 10;
nums[2 * i + 1] |= (nums[n + i] & 0b1111111111) << 10;
}
// 將結果右移10位,低10位的原值沒用了
for (int i = 0; i < n; ++i) {
nums[2 * i] >>= 10;
nums[2 * i + 1] >>= 10;
}
return nums;
}
}
c
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
// 申請新的陣列,這樣用空間換時間
int *ans = (int *) malloc(sizeof(int) * numsSize);
for (int i = 0; i < n; ++i) {
ans[2 * i] = nums[i];
ans[2 * i + 1] = nums[n + i];
}
*returnSize = numsSize;
return ans;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
// 原地排列,時間換空間
for (int i = 0; i < n; ++i) {
// 把重排列的值左移10位
nums[2 * i] |= (nums[i] & 0b1111111111) << 10;
nums[2 * i + 1] |= (nums[n + i] & 0b1111111111) << 10;
}
// 將結果右移10位,低10位的原值沒用了
for (int i = 0; i < n; ++i) {
nums[2 * i] >>= 10;
nums[2 * i + 1] >>= 10;
}
*returnSize = numsSize;
return nums;
}
c++
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
// 申請新的陣列,這樣用空間換時間
vector<int> ans;
for (int i = 0; i < n; ++i) {
ans.push_back(nums[i]);
ans.push_back(nums[n + i]);
}
return ans;
}
};
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
// 原地排列,時間換空間
for (int i = 0; i < n; ++i) {
// 把重排列的值左移10位
nums[2 * i] |= (nums[i] & 0b1111111111) << 10;
nums[2 * i + 1] |= (nums[n + i] & 0b1111111111) << 10;
}
// 將結果右移10位,低10位的原值沒用了
for (int i = 0; i < n; ++i) {
nums[2 * i] >>= 10;
nums[2 * i + 1] >>= 10;
}
return nums;
}
};
python
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
# 申請新的陣列,這樣用空間換時間
ans = []
for i in range(n):
ans.append(nums[i])
ans.append(nums[n + i])
return ans
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
# 原地排列,時間換空間
for i in range(n):
# 把重排列的值左移10位
nums[2 * i] |= (nums[i] & 0b1111111111) << 10;
nums[2 * i + 1] |= (nums[n + i] & 0b1111111111) << 10
# 將結果右移10位,低10位的原值沒用了
for i in range(n):
nums[2 * i] >>= 10
nums[2 * i + 1] >>= 10
return nums
go
func shuffle(nums []int, n int) []int {
// 申請新的陣列,這樣用空間換時間
ans := make([]int, 0, n*2)
for i := 0; i < n; i++ {
ans = append(ans, nums[i], nums[n+i])
}
return ans
}
func shuffle(nums []int, n int) []int {
// 原地排列,時間換空間
for i := 0; i < n; i++ {
// 把重排列的值左移10位
nums[2*i] |= (nums[i] & 0b1111111111) << 10
nums[2*i+1] |= (nums[n+i] & 0b1111111111) << 10
}
// 將結果右移10位,低10位的原值沒用了
for i := 0; i < n; i++ {
nums[2*i] >>= 10
nums[2*i+1] >>= 10
}
return nums
}
rust
impl Solution {
pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
let n: usize = n as usize;
// 申請新的陣列,這樣用空間換時間
let mut ans = Vec::new();
(0..n).for_each(|i| {
ans.push(nums[i]);
ans.push(nums[n + i]);
});
ans
}
}
impl Solution {
pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
let mut nums = nums;
let n = n as usize;
// 原地排列,時間換空間
(0..n).for_each(|i| {
// 把重排列的值左移10位
nums[2 * i] |= (nums[i] & 0b1111111111) << 10;
nums[2 * i + 1] |= (nums[n + i] & 0b1111111111) << 10;
});
// 將結果右移10位,低10位的原值沒用了
(0..n).for_each(|i| {
nums[2 * i] >>= 10;
nums[2 * i + 1] >>= 10;
});
nums
}
}

原題傳送門:https://leetcode-cn.com/problems/shuffle-the-array/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/342222.html
標籤:python
