forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuffleArray.cs
More file actions
34 lines (30 loc) · 761 Bytes
/
ShuffleArray.cs
File metadata and controls
34 lines (30 loc) · 761 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
// Problem 670: https://leetcode.com/explore/interview/card/top-interview-questions-easy/98/design/670/
public class ShuffleArray
{
int[] arrayOriginal;
int[] arrayCopy;
public ShuffleArray(int[] nums)
{
arrayOriginal = (int[])nums.Clone();
arrayCopy = nums;
}
public int[] Reset()
{
return arrayOriginal;
}
public int[] Shuffle()
{
Random random = new Random();
for (int i = 0; i < arrayCopy.Length; i++)
{
swapPlaces(i, random.Next(0, arrayCopy.Length));
}
return arrayCopy;
}
private void swapPlaces(int i, int j)
{
int temp = arrayCopy[i];
arrayCopy[i] = arrayCopy[j];
arrayCopy[j] = temp;
}
}