20250120 finished.
This commit is contained in:
parent
d6cb6aae1f
commit
465899b94f
|
@ -442,3 +442,5 @@ mod p3097_shortest_subarray_with_or_at_least_k_ii;
|
||||||
mod p3287_find_the_maximum_sequence_value_of_array;
|
mod p3287_find_the_maximum_sequence_value_of_array;
|
||||||
|
|
||||||
mod p2266_count_number_of_texts;
|
mod p2266_count_number_of_texts;
|
||||||
|
|
||||||
|
mod p2239_find_closest_number_to_zero;
|
||||||
|
|
34
src/problem/p2239_find_closest_number_to_zero.rs
Normal file
34
src/problem/p2239_find_closest_number_to_zero.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/**
|
||||||
|
* [2239] Find Closest Number to Zero
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn find_closest_number(nums: Vec<i32>) -> i32 {
|
||||||
|
nums.into_iter().fold(i32::MAX, |acc, v| {
|
||||||
|
if v.abs() < acc.abs() {
|
||||||
|
v
|
||||||
|
} else if v.abs() == acc.abs() && v > acc {
|
||||||
|
v
|
||||||
|
} else {
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2239() {
|
||||||
|
assert_eq!(-1, Solution::find_closest_number(vec![-1, -1]));
|
||||||
|
assert_eq!(1, Solution::find_closest_number(vec![-4, -2, 1, 4, 8]));
|
||||||
|
assert_eq!(1, Solution::find_closest_number(vec![-2, -1, 1]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user