24.01.2023, 15:34
Shuffle the Array - LeetCode
https://leetcode.com/problems/shuffle-the-array/
1/1
Description
Discussion (20)
Solutions (4.1K
Easy
3.4K
221
Given the array
nums
consisting of
2n
elements in the form
[x ,x ,...,x ,y ,y ,...,y ]
.
Return the array in the form
[x ,y ,x ,y ,...,x ,y ]
.
Example 1:
Input:
nums =
[2,5,1,3,4,7], n = 3
Output:
[2,3,5,4,1,7]
Explanation:
Since x =2,
x =5, x =1, y =3, y =4, y =7
then the answer is
[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]
Example 3:
Input:
nums = [1,1,2,2], n
= 2
Output:
[1,2,1,2]
Constraints:
1 <= n <= 500
Companies
1
2
n
1
2
n
1
1
2
2
n
n
1
2
3
1
2
3
nums =
[2,5,1,3,4,7]
n =
3
Console
Run
Submit
Auto
Java
1
2
4
6
7
8
3
5
9
class
Solution
{
public
int
[]
shuffle
(
int
[]
nums
,
int
n
) {
for
(
int
i
=
0
; i < n; i ++) {
System
.
arraycopy
(arr,
0
, sh,i, );
}
}
int
[]
sh
=
new
int
[
nums
.
length
];
int
[]
arr
= {nums[i], nums[i + n]};
}
Testcase
Result
Case
Case
Case
Do'stlaringiz bilan baham: |