File tree Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ fn main() {}
5
5
* let obj = Solution::new(nums);
6
6
* let ret_1: i32 = obj.pick(target);
7
7
*/
8
- struct Solution {
8
+ struct Solution1 {
9
9
index_map : std:: collections:: HashMap < i32 , Vec < usize > >
10
10
}
11
11
@@ -14,7 +14,7 @@ struct Solution {
14
14
* `&self` means the method takes an immutable reference.
15
15
* If you need a mutable reference, change it to `&mut self` instead.
16
16
*/
17
- impl Solution {
17
+ impl Solution1 {
18
18
fn new ( nums : Vec < i32 > ) -> Self {
19
19
let mut index_map = std:: collections:: HashMap :: new ( ) ;
20
20
@@ -40,3 +40,30 @@ impl Solution {
40
40
}
41
41
}
42
42
}
43
+
44
+ struct Solution {
45
+ nums : Vec < i32 >
46
+ }
47
+
48
+ impl Solution {
49
+ fn new ( nums : Vec < i32 > ) -> Self {
50
+ Self { nums }
51
+ }
52
+
53
+ fn pick ( & self , target : i32 ) -> i32 {
54
+ use rand:: Rng ;
55
+ let mut n = 0 ;
56
+ let mut r = 0 ;
57
+
58
+ for i in 0 ..self . nums . len ( ) {
59
+ if self . nums [ i] == target {
60
+ n += 1 ;
61
+ if rand:: thread_rng ( ) . gen_range ( 0 ..n) == 0 {
62
+ r = i as i32 ;
63
+ }
64
+ }
65
+ }
66
+
67
+ r
68
+ }
69
+ }
You can’t perform that action at this time.
0 commit comments