-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshuffle-an-array.rs
40 lines (34 loc) · 963 Bytes
/
shuffle-an-array.rs
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
#![allow(dead_code, unused, unused_variables)]
fn main() {}
/**
* Your Solution object will be instantiated and called as such:
* let obj = Solution::new(nums);
* let ret_1: Vec<i32> = obj.reset();
* let ret_2: Vec<i32> = obj.shuffle();
*/
struct Solution {
nums: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Solution {
fn new(nums: Vec<i32>) -> Self {
Self { nums }
}
/** Resets the array to its original configuration and return it. */
fn reset(&self) -> Vec<i32> {
self.nums.clone()
}
/** Returns a random shuffling of the array. */
fn shuffle(&self) -> Vec<i32> {
use rand::Rng;
let mut rng = rand::thread_rng();
let mut v = self.nums.clone();
for i in 0..self.nums.len() {
v.swap(rng.gen_range(0..self.nums.len()), i);
}
v
}
}