From 51457548484d22a9a840fb1acda8855bd16b9299 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Wed, 29 Nov 2023 17:15:27 +0800 Subject: [PATCH] lc: Fix lint warning --- leetcode/704.binary_search/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/leetcode/704.binary_search/src/main.rs b/leetcode/704.binary_search/src/main.rs index 23be6a82..eb310ab6 100644 --- a/leetcode/704.binary_search/src/main.rs +++ b/leetcode/704.binary_search/src/main.rs @@ -4,6 +4,8 @@ //! Problem: [binary search](https://leetcode.com/problems/binary-search) +use std::cmp::Ordering; + pub fn solution1(nums: &[i32], target: i32) -> i32 { if nums.is_empty() { return -1; @@ -13,13 +15,11 @@ pub fn solution1(nums: &[i32], target: i32) -> i32 { let mut high = nums.len() - 1; while low + 1 < high { let middle = low + (high - low) / 2; - println!("low: {low}, high: {high}, middle: {middle}"); - if nums[middle] < target { - low = middle; - } else if nums[middle] == target { - return middle as i32; - } else if nums[middle] > target { - high = middle; + //println!("low: {low}, high: {high}, middle: {middle}"); + match nums[middle].cmp(&target) { + Ordering::Less => low = middle, + Ordering::Equal => return middle as i32, + Ordering::Greater => high = middle, } } -1