-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuffleArray.php
More file actions
37 lines (35 loc) · 814 Bytes
/
Copy pathShuffleArray.php
File metadata and controls
37 lines (35 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace App;
/**
* Shuffle the Array
*
* Given the array nums consisting of 2n elements in the form [x_1,x_2,...,x_n,y_1,y_2,...,y_n].
* Return the array in the form [x_1,y_1,x_2,y_2,...,x_n,y_n].
*
* Example 1:
* Input: nums = [2,5,1,3,4,7], n = 3
* Output: [2,3,5,4,1,7]
*
* Example 2:
* Input: nums = [1,2,3,4,4,3,2,1], n = 4
* Output: [1,4,2,3,3,2,4,1]
*
* https://leetcode.com/problems/shuffle-the-array
*/
class ShuffleArray
{
/**
* @param array<array-key, int> $nums
* @param int $n
* @return array<array-key, int>
*/
public function shuffle(array $nums, int $n): array
{
$result = [];
for ($i = 0; $i < $n; $i++) {
$result[] = $nums[$i];
$result[] = $nums[$i + $n ];
}
return $result;
}
}