20250217 finished.

This commit is contained in:
jackfiled 2025-04-25 14:06:46 +08:00
parent 7263a2e7f0
commit 5751d9d82e
2 changed files with 49 additions and 0 deletions

View File

@ -490,3 +490,5 @@ mod p1552_magnetic_force_between_two_balls;
mod p1706_where_will_the_ball_fall; mod p1706_where_will_the_ball_fall;
mod p1299_replace_elements_with_greatest_element_on_right_side; mod p1299_replace_elements_with_greatest_element_on_right_side;
mod p1287_element_appearing_more_than_25_in_sorted_array;

View File

@ -0,0 +1,47 @@
use std::thread::current;
/**
* [1287] Element Appearing More Than 25% In Sorted Array
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn find_special_integer(arr: Vec<i32>) -> i32 {
let mut last = arr[0];
let mut count = 1;
let threshold = arr.len() / 4;
for &i in arr[1..].iter() {
if last == i {
count += 1;
} else {
last = i;
count = 1;
}
if count > threshold {
return i;
}
}
last
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1287() {
assert_eq!(
6,
Solution::find_special_integer(vec![1, 2, 2, 6, 6, 6, 6, 7, 10])
);
assert_eq!(1, Solution::find_special_integer(vec![1, 1]));
}
}