-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0384_shuffle_an_array.rs
More file actions
59 lines (51 loc) · 1.36 KB
/
s0384_shuffle_an_array.rs
File metadata and controls
59 lines (51 loc) · 1.36 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
58
59
#![allow(unused)]
use rand::{rngs::ThreadRng, thread_rng, Rng};
pub struct Solution {
original: 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 {
original: nums.clone(),
}
}
/** Resets the array to its original configuration and return it. */
fn reset(&self) -> Vec<i32> {
self.original.clone()
}
/** Returns a random shuffling of the array. */
fn shuffle(&self) -> Vec<i32> {
let mut ans = self.original.clone();
let len = ans.len();
let mut rng = thread_rng();
for i in 0..len {
self.swap_at(&mut ans, i, rng.gen_range(i, len));
}
ans
}
fn swap_at(&self, ans: &mut Vec<i32>, a: usize, b: usize) {
let tmp = ans[a];
ans[a] = ans[b];
ans[b] = tmp;
}
}
/**
* 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();
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_384() {
let obj = Solution::new(vec![1, 2, 3]);
// assert_eq!(obj.shuffle(), vec![3, 1, 2]);
assert_eq!(obj.reset(), vec![1, 2, 3]);
}
}