Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 49fd41c

Browse files
author
guangsheng.li01
committed
Solved 1287
1 parent 130882b commit 49fd41c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* [1287] element-appearing-more-than-25-in-sorted-array
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn find_special_integer(arr: Vec<i32>) -> i32 {
11+
let t = arr.len() / 4;
12+
for i in 0..arr.len() - 1 {
13+
if arr[i] == arr[i + t] {
14+
return arr[i];
15+
}
16+
}
17+
arr[0]
18+
}
19+
}
20+
21+
// solution impl ends here
22+
23+
// solution tests starts here
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_case0() {
31+
assert_eq!(
32+
Solution::find_special_integer(vec![1, 2, 2, 6, 6, 6, 6, 7, 10]),
33+
6
34+
);
35+
}
36+
37+
#[test]
38+
fn test_case1() {
39+
assert_eq!(
40+
Solution::find_special_integer(vec![1, 1, 2, 2, 3, 3, 3, 3]),
41+
3
42+
);
43+
}
44+
45+
#[test]
46+
fn test_case2() {
47+
assert_eq!(Solution::find_special_integer(vec![1, 2, 3, 3]), 3);
48+
}
49+
}
50+
51+
// solution tests ends here

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ mod a0929_unique_email_addresses;
1616
mod a0937_reorder_data_in_log_files;
1717
mod a1047_remove_all_adjacent_duplicates_in_string;
1818
mod a1200_minimum_absolute_difference;
19+
mod a1287_element_appearing_more_than_25_in_sorted_array;
1920
mod a1317_convert_integer_to_the_sum_of_two_no_zero_integers;

0 commit comments

Comments
 (0)