From 64dcfc583b2976e6e1ea500c8f965cf66413d7ad Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Mon, 9 Oct 2023 01:07:29 -0400 Subject: [PATCH] adding the binary search code --- lectures/12_advanced_recursion/README.md | 2 +- leetcode/p704_binarysearch.v1.cpp | 52 ++++++++++++++++++++++++ leetcode/p704_binarysearch.v2.cpp | 32 +++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 leetcode/p704_binarysearch.v1.cpp create mode 100644 leetcode/p704_binarysearch.v2.cpp diff --git a/lectures/12_advanced_recursion/README.md b/lectures/12_advanced_recursion/README.md index 729659a..4d164cd 100644 --- a/lectures/12_advanced_recursion/README.md +++ b/lectures/12_advanced_recursion/README.md @@ -136,4 +136,4 @@ a recursive call is made. ## 12.10 Leetcode Exercises -- [Leetcode problem 704: Binary Search](https://leetcode.com/problems/binary-search/). Solution: [p141_linkedlistcycle.cpp](../../leetcode/p141_linkedlistcycle.cpp) +- [Leetcode problem 704: Binary Search](https://leetcode.com/problems/binary-search/). Solution: [p704_binarysearch.v1.cpp](../../leetcode/p704_binarysearch.v1.cpp)a or [p704_binarysearch.v2.cpp](../../leetcode/p704_binarysearch.v2.cpp) diff --git a/leetcode/p704_binarysearch.v1.cpp b/leetcode/p704_binarysearch.v1.cpp new file mode 100644 index 0000000..a8c1179 --- /dev/null +++ b/leetcode/p704_binarysearch.v1.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + bool search(vector& nums, int low, int high, int target, int& index){ + // this is better than mid=(high+low)/2 because the latter might overflow when high and low are very large integers. + // caution: if low = high - 1, then mid = low. and this could lead to an infinite loop situation. and because of this, we should never search the range [mid, high], because that actually means search the range of [low, high], which is a repeat of the existing call. + int mid = low + (high-low)/2; + // base case + if(low>=high){ + index = low; + return (target == nums[low]); + } + // general case + if(target<=nums[mid]){ + // search the first half + // and because now we need to pass the range, we have to write another function. + return search(nums, low, mid, target, index); + }else{ + // search the second half + return search(nums, mid+1, high, target, index); + } + } + + int search(vector& nums, int target) { + int index; + int size = nums.size(); + // code below should go into the other search() function, but I write the code like this so as to help you to see how the idea of creating the other search() function was formed. + int mid = size/2; + if(size == 1){ + if(target == nums[0]){ + return 0; + }else{ + return -1; + } + } + if(target<=nums[mid]){ + // search the first half + // because now we need to pass the range, we have to write another function. + if(search(nums, 0, mid, target, index) == true){ + return index; + }else{ + return -1; + } + }else{ + // search the second half + if(search(nums, mid+1, size-1, target, index) == true){ + return index; + }else{ + return -1; + } + } + } +}; diff --git a/leetcode/p704_binarysearch.v2.cpp b/leetcode/p704_binarysearch.v2.cpp new file mode 100644 index 0000000..f12cc77 --- /dev/null +++ b/leetcode/p704_binarysearch.v2.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + bool search(vector& nums, int low, int high, int target, int& index){ + // this is better than mid=(high+low)/2 because the latter might overflow when high and low are very large integers. + // caution: if low = high - 1, then mid = low. and this could lead to an infinite loop situation. and because of this, we should never search the range [mid, high], because that actually means search the range of [low, high], which is a repeat of the existing call. + int mid = low + (high-low)/2; + // base case + if(low>=high){ + index = low; + return (target == nums[low]); + } + // general case + if(target<=nums[mid]){ + // search the first half + // and because now we need to pass the range, we have to write another function. + return search(nums, low, mid, target, index); + }else{ + // search the second half + return search(nums, mid+1, high, target, index); + } + } + // the driver function + int search(vector& nums, int target) { + int index; + int size = nums.size(); + if(search(nums, 0, size-1, target, index) == true){ + return index; + }else{ + return -1; + } + } +};