20250120 finished.

This commit is contained in:
jackfiled 2025-01-20 11:28:28 +08:00
parent d6cb6aae1f
commit 465899b94f
2 changed files with 36 additions and 0 deletions

View File

@ -442,3 +442,5 @@ mod p3097_shortest_subarray_with_or_at_least_k_ii;
mod p3287_find_the_maximum_sequence_value_of_array;
mod p2266_count_number_of_texts;
mod p2239_find_closest_number_to_zero;

View 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]));
}
}