384. Shuffle an Array

  • 45.4%

https://leetcode.com/problems/shuffle-an-array/

Shuffle a set of numbers without duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> Example:
>
> // Init an array with set 1, 2, and 3.
> int[] nums = {1,2,3};
> Solution solution = new Solution(nums);
>
> // Shuffle the array [1,2,3] and return its result.
> Any permutation of [1,2,3] must equally likely to be returned.
> solution.shuffle();
>
> // Resets the array back to its original configuration [1,2,3].
> solution.reset();
>
> // Returns the random shuffling of array [1,2,3].
> solution.shuffle();
>

java

314ms, September 13, 2016

https://discuss.leetcode.com/topic/53978/first-accepted-solution-java

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
38
39
40
import java.util.Random;

public class Solution {
private int[] nums;
private Random random;

public Solution(int[] nums) {
this.nums = nums;
random = new Random();
}

/** Resets the array to its original configuration and return it. */
public int[] reset() {
return nums;
}

/** Returns a random shuffling of the array. */
public int[] shuffle() {
if(nums == null) return null;
int[] a = nums.clone();
for(int j = 1; j< a.length; j++){
int i = random.nextInt(j+1);
swap(a, i, j);
}
return a;
}

private void swap(int[] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}

/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.reset();
* int[] param_2 = obj.shuffle();
*/

cpp

375ms, September 13, 2016

https://discuss.leetcode.com/topic/53979/straight-forward-c-solution

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
class Solution {
vector<int> nums;
public:
Solution(vector<int> nums) {
this->nums = nums;
}

/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return nums;
}

/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> result(nums);
for(int i = 0; i<result.size(); i++){
int pos = rand()%(result.size()-i);
swap(result[i+pos], result[i]);
}
return result;
}
};

/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* vector<int> param_1 = obj.reset();
* vector<int> param_2 = obj.shuffle();
*/

python

688ms, September 13, 2016

https://discuss.leetcode.com/topic/53994/straight-forward-python-solution

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
from random import shuffle
class Solution(object):

def __init__(self, nums):
"""

:type nums: List[int]
:type size: int
"""
self.nums = nums
self.l = len(nums)
self.ord = range(self.l)

def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
self.ord = range(self.l)
return self.nums


def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
shuffle(self.ord)
return [self.nums[i] for i in self.ord]



# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
谢谢你,可爱的朋友。