leetcode384

# 小郑之家~

### 实现1.

class Solution(object):

def __init__(self, nums):
"""
:type nums: List[int]
"""
self.nums = nums
ori = []
ori[:] = nums
self.ori = ori

def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
return self.ori

def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
if len(self.nums)==0:
return []
import numpy as np
idx = np.random.choice(range(len(self.nums)), len(self.nums), replace=False)
return [self.nums[i] for i in idx]



### 实现2

import random

class Solution:

def __init__(self, nums):
self.nums = nums

def reset(self):
return self.nums

def shuffle(self):
return sorted(self.nums, key=lambda x: random.random())



class Solution(object):

def __init__(self, nums):
"""
:type nums: List[int]
"""
self.nums = nums

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

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



[4, 8, 3, 0, 5, 1, 2, 7, 9, 6]
[4, 8, 3, 0, 5, 1, 2, 7, 9, 6]
[1, 6, 9, 0, 4, 7, 2, 3, 8, 5]
[1, 6, 9, 0, 4, 7, 2, 3, 8, 5]



shuffle-->reset-->shuffle-->reset调的。传入的是range(10).

class Solution(object):

def __init__(self, nums):
"""
:type nums: List[int]
"""
self.nums = nums

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

def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
import random
a = []
a[:]=self.nums
random.shuffle(a)
return a



### c++

class Solution {
vector<int> ori;
public:
Solution(vector<int> nums):ori{nums}{

}

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

}

/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> a{ori};
random_shuffle(a.begin(), a.end());
return a;
}
};