-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_1.py
More file actions
57 lines (45 loc) · 1.21 KB
/
question_1.py
File metadata and controls
57 lines (45 loc) · 1.21 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/24 20:38
# @Author : cancan
# @File : question_1.py
# @Function : 打乱数组
"""
Question:
打乱一个没有重复元素的数组。
Example:
// 以数字集合 1, 2 和 3 初始化数组。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);
// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
solution.shuffle();
// 重设数组到它的初始状态[1,2,3]。
solution.reset();
// 随机返回数组[1,2,3]打乱后的结果。
solution.shuffle();
"""
from random import shuffle
class Solution:
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.old = nums
self.t = nums[:]
def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
return self.old
def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
shuffle(self.t)
return self.t
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()