-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0398_random_pick_index.rs
More file actions
52 lines (46 loc) · 1.39 KB
/
s0398_random_pick_index.rs
File metadata and controls
52 lines (46 loc) · 1.39 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
#![allow(unused)]
use rand::{rngs::ThreadRng, thread_rng, Rng};
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 }
}
fn pick(&self, target: i32) -> i32 {
let mut total = 0;
let mut rng = thread_rng();
let mut res = -1;
for i in 0..self.nums.len() {
if self.nums[i] == target {
// randomly select an int from 0 to the nums of target. If x equals 0,
// set the res as the current index. The probability is always 1/nums for the
// latest appeared number. For example, 1 for 1st num, 1/2 for 2nd num,
// 1/3 for 3nd num (1/2 * 2/3 for each of the first 2 nums).
total += 1;
if rng.gen_range(0, total) == 0 {
res = i as i32;
}
}
}
res
}
}
/**
* Your Solution object will be instantiated and called as such:
* let obj = Solution::new(nums);
* let ret_1: i32 = obj.pick(target);
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_398() {
// let obj = Solution::new(vec![1, 2, 3, 3, 3]);
// assert!(obj.pick(3) == 2 || obj.pick(3) == 3 || obj.pick(3) == 4);
}
}