Bin search

This commit is contained in:
Sainnhe Park 2022-11-24 16:44:36 +08:00
parent 8f3ebbdd3d
commit 2822593c46
11 changed files with 256 additions and 120 deletions

View File

@ -2,13 +2,16 @@
#define S0034_FIND_FIRST_AND_LAST_POSITION_OF_ELEMENT_IN_SORTED_ARRAY_HPP #define S0034_FIND_FIRST_AND_LAST_POSITION_OF_ELEMENT_IN_SORTED_ARRAY_HPP
#include <vector> #include <vector>
#include <cmath>
using namespace std; using namespace std;
class Solution { class Solution1 {
public: public:
vector<int> searchRange(vector<int>& nums, int target); vector<int> searchRange(vector<int>& nums, int target);
}; };
class Solution2 {
public:
vector<int> searchRange(vector<int>& nums, int target);
};
#endif #endif

View File

@ -2,11 +2,15 @@
#define S0035_SEARCH_INSERT_POSITION_HPP #define S0035_SEARCH_INSERT_POSITION_HPP
#include <vector> #include <vector>
#include <cmath>
using namespace std; using namespace std;
class Solution { class Solution1 {
public:
int searchInsert(vector<int>& nums, int target);
};
class Solution2 {
public: public:
int searchInsert(vector<int>& nums, int target); int searchInsert(vector<int>& nums, int target);
}; };

View File

@ -0,0 +1,18 @@
#ifndef S0704_BINARY_SEARCH_HPP
#define S0704_BINARY_SEARCH_HPP
#include <vector>
using namespace std;
class Solution1 {
public:
int binSearch(vector<int>& nums, int target);
};
class Solution2 {
public:
int binSearch(vector<int>& nums, int target);
};
#endif

View File

@ -1,9 +1,9 @@
# 思路 # 数组
- [深度优先遍历](./dfs.md) - [二分查找](./bin_search.md)
- [广度优先遍历](./bfs.md)
## 经典代码 # 经典代码
- [合并两个有序链表](./merge_two_sorted_linked_lists.md) - [合并两个有序链表](./merge_two_sorted_linked_lists.md)
- [二分查找](./bin_search.md) - [深度优先遍历](./dfs.md)
- [广度优先遍历](./bfs.md)

View File

@ -1,57 +1,64 @@
# 二分查找 # 二分查找
非递归: 适用于数组有序的情况下查找目标值。
## 写法一
针对左闭右闭区间(即 `[left, right]`
```cpp ```cpp
/** class Solution {
* 二分查找普通实现。 public:
* @param srcArray 有序数组 int search(vector<int>& nums, int target) {
* @param key 查找元素 int left = 0;
* @return 不存在返回-1 int right = nums.size() - 1; // 定义target在左闭右闭的区间里[left, right]
*/ while (left <=
public right) { // 当left==right区间[left, right]依然有效,所以用 <=
static int binSearch(int srcArray[], int key) { int middle =
int mid; left + ((right - left) / 2); // 防止溢出 等同于(left + right)/2
int start = 0; if (nums[middle] > target) {
int end = srcArray.length - 1; right = middle - 1; // target 在左区间,所以[left, middle - 1]
while (start <= end) { } else if (nums[middle] < target) {
mid = (end - start) / 2 + start; left = middle + 1; // target 在右区间,所以[middle + 1, right]
if (key < srcArray[mid]) { } else { // nums[middle] == target
end = mid - 1; return middle; // 数组中找到目标值,直接返回下标
} else if (key > srcArray[mid]) { }
start = mid + 1;
} else {
return mid;
} }
// 未找到目标值
return -1;
} }
return -1; };
}
``` ```
递归: ## 写法二
针对左闭右开(即 `[left, right)`
```cpp ```cpp
/** class Solution {
* 二分查找递归实现。 public:
* @param srcArray 有序数组 int search(vector<int>& nums, int target) {
* @param start 数组低地址下标 int left = 0;
* @param end 数组高地址下标 int right = nums.size(); // 定义target在左闭右开的区间里[left, right)
* @param key 查找元素 while (left < right) { // 因为left == right的时候[left,
* @return 查找元素不存在返回-1 // right)是无效的空间,所以使用 <
*/ int middle = left + ((right - left) >> 1);
public if (nums[middle] > target) {
static int binSearch(int srcArray[], int start, int end, int key) { right = middle; // target 在左区间,在[left, middle)中
int mid = (end - start) / 2 + start; } else if (nums[middle] < target) {
if (srcArray[mid] == key) { left = middle + 1; // target 在右区间,在[middle + 1, right)中
return mid; } else { // nums[middle] == target
} return middle; // 数组中找到目标值,直接返回下标
if (start >= end) { }
}
// 未找到目标值
return -1; return -1;
} else if (key > srcArray[mid]) {
return binSearch(srcArray, mid + 1, end, key);
} else if (key < srcArray[mid]) {
return binSearch(srcArray, start, mid - 1, key);
} }
return -1; };
}
``` ```
## 相关题目
- [704. 二分查找](https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#_704-%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE)
- [35. 搜索插入位置](https://programmercarl.com/0035.%E6%90%9C%E7%B4%A2%E6%8F%92%E5%85%A5%E4%BD%8D%E7%BD%AE.html#_35-%E6%90%9C%E7%B4%A2%E6%8F%92%E5%85%A5%E4%BD%8D%E7%BD%AE)
- [34. 在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html#_34-%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE)

View File

@ -1,38 +1,66 @@
#include "s0034_find_first_and_last_position_of_element_in_sorted_array.hpp" #include "s0034_find_first_and_last_position_of_element_in_sorted_array.hpp"
vector<int> Solution::searchRange(vector<int>& nums, int target) { // 闭区间写法
int size = nums.size(); vector<int> Solution1::searchRange(vector<int>& nums, int target) {
if (size <= 0) { int len = nums.size();
return vector<int> {-1, -1}; int l{0};
} else if (size == 1) { int r = len - 1;
if (nums[0] == target) { while (l <= r) {
return vector<int> {0, 0}; int m = (l + r) >> 1;
if (target < nums[m]) {
r = m - 1;
} else if (nums[m] < target) {
l = m + 1;
} else { } else {
return vector<int> {-1, -1}; l = r = m;
} while (l >= 0) {
} if (nums[l] == target) {
int left{0}; --l;
int right = size - 1; } else {
int mid; break;
while (left <= right) {
mid = static_cast<int>(floor((left + right) / 2));
if (nums[mid] == target) {
int resultLeft, resultRight;
for (int i = mid; i < size; ++i) {
if (nums[i] == target) {
resultRight = i;
} }
} }
for (int i = mid; i >= 0; --i) { while (r <= len - 1) {
if (nums[i] == target) { if (nums[r] == target) {
resultLeft = i; ++r;
} else {
break;
} }
} }
return vector<int> {resultLeft, resultRight}; return vector<int> {l + 1, r - 1};
} else if (nums[mid] > target) { }
right = mid - 1; }
} else { return vector<int> {-1, -1};
left = mid + 1; }
// 开区间写法
vector<int> Solution2::searchRange(vector<int>& nums, int target) {
int len = nums.size();
int l{0};
int r = len;
while (l < r) {
int m = (l + r) >> 1;
if (target < nums[m]) {
r = m;
} else if (nums[m] < target) {
l = m + 1;
} else {
l = r = m;
while (l >= 0) {
if (nums[l] == target) {
--l;
} else {
break;
}
}
while (r <= len - 1) {
if (nums[r] == target) {
++r;
} else {
break;
}
}
return vector<int> {l + 1, r - 1};
} }
} }
return vector<int> {-1, -1}; return vector<int> {-1, -1};

View File

@ -1,33 +1,37 @@
#include "s0035_search_insert_position.hpp" #include "s0035_search_insert_position.hpp"
int Solution::searchInsert(vector<int>& nums, int target) { // 闭区间写法
int size = nums.size(); int Solution1::searchInsert(vector<int>& nums, int target) {
if (size == 0) { int len = nums.size();
return 0; int l{0};
} int r = len - 1;
int left{0}; while (l <= r) {
int right = size - 1; int m = (l + r) >> 1;
int mid; if (target < nums[m]) {
while (left <= right) { r = m - 1;
mid = static_cast<int>(floor((left + right) / 2)); } else if (nums[m] < target) {
if (nums[mid] == target) { l = m + 1;
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else { } else {
left = mid + 1; return m;
} }
} }
if (right < 0) { return r + 1;
return 0; }
} else if (left > size - 1) {
return size; // 开区间写法
} int Solution2::searchInsert(vector<int>& nums, int target) {
if (target < nums[right]) { int len = nums.size();
return right; int l{0};
} else if (target > nums[left]) { int r = len;
return left + 1; while (l < r) {
} else { int m = (l + r) >> 1;
return left; if (target < nums[m]) {
} r = m;
} else if (nums[m] < target) {
l = m + 1;
} else {
return m;
}
}
return r;
} }

View File

@ -0,0 +1,37 @@
#include "s0704_binary_search.hpp"
// 闭区间写法
int Solution1::binSearch(vector<int>& nums, int target) {
int len = nums.size();
int l{0};
int r = len - 1;
while (l <= r) {
int m = (l + r) >> 1;
if (target < nums[m]) {
r = m - 1;
} else if (nums[m] < target) {
l = m + 1;
} else {
return m;
}
}
return -1;
}
// 开区间写法
int Solution2::binSearch(vector<int>& nums, int target) {
int len = nums.size();
int l{0};
int r = len;
while (l < r) {
int m = (l + r) >> 1;
if (target < nums[m]) {
r = m;
} else if (nums[m] < target) {
l = m + 1;
} else {
return m;
}
}
return -1;
}

View File

@ -6,22 +6,28 @@ TEST(Problem34, Case1) {
vector<int> nums{5, 7, 7, 8, 8, 10}; vector<int> nums{5, 7, 7, 8, 8, 10};
int target{8}; int target{8};
vector<int> o{3, 4}; vector<int> o{3, 4};
Solution solution; Solution1 solution1;
EXPECT_EQ(solution.searchRange(nums, target), o); Solution2 solution2;
EXPECT_EQ(solution1.searchRange(nums, target), o);
EXPECT_EQ(solution2.searchRange(nums, target), o);
} }
TEST(Problem34, Case2) { TEST(Problem34, Case2) {
vector<int> nums{5, 7, 7, 8, 8, 10}; vector<int> nums{5, 7, 7, 8, 8, 10};
int target{6}; int target{6};
vector<int> o{-1, -1}; vector<int> o{-1, -1};
Solution solution; Solution1 solution1;
EXPECT_EQ(solution.searchRange(nums, target), o); Solution2 solution2;
EXPECT_EQ(solution1.searchRange(nums, target), o);
EXPECT_EQ(solution2.searchRange(nums, target), o);
} }
TEST(Problem34, Case3) { TEST(Problem34, Case3) {
vector<int> nums{}; vector<int> nums{};
int target{0}; int target{0};
vector<int> o{-1, -1}; vector<int> o{-1, -1};
Solution solution; Solution1 solution1;
EXPECT_EQ(solution.searchRange(nums, target), o); Solution2 solution2;
EXPECT_EQ(solution1.searchRange(nums, target), o);
EXPECT_EQ(solution2.searchRange(nums, target), o);
} }

View File

@ -6,22 +6,28 @@ TEST(Problem35, Case1) {
vector<int> nums{1, 3, 5, 6}; vector<int> nums{1, 3, 5, 6};
int target{5}; int target{5};
int o{2}; int o{2};
Solution solution; Solution1 solution1;
EXPECT_EQ(solution.searchInsert(nums, target), o); Solution2 solution2;
EXPECT_EQ(solution1.searchInsert(nums, target), o);
EXPECT_EQ(solution2.searchInsert(nums, target), o);
} }
TEST(Problem35, Case2) { TEST(Problem35, Case2) {
vector<int> nums{1, 3, 5, 6}; vector<int> nums{1, 3, 5, 6};
int target{2}; int target{2};
int o{1}; int o{1};
Solution solution; Solution1 solution1;
EXPECT_EQ(solution.searchInsert(nums, target), o); Solution2 solution2;
EXPECT_EQ(solution1.searchInsert(nums, target), o);
EXPECT_EQ(solution2.searchInsert(nums, target), o);
} }
TEST(Problem35, Case3) { TEST(Problem35, Case3) {
vector<int> nums{1, 3, 5, 6}; vector<int> nums{1, 3, 5, 6};
int target{7}; int target{7};
int o{4}; int o{4};
Solution solution; Solution1 solution1;
EXPECT_EQ(solution.searchInsert(nums, target), o); Solution2 solution2;
EXPECT_EQ(solution1.searchInsert(nums, target), o);
EXPECT_EQ(solution2.searchInsert(nums, target), o);
} }

View File

@ -0,0 +1,23 @@
#include "s0704_binary_search.hpp"
#include <gtest/gtest.h>
TEST(Problem704, Case1) {
vector<int> nums{-1, 0, 3, 5, 9, 12};
int target{9};
int o{4};
Solution1 solution1;
Solution2 solution2;
EXPECT_EQ(solution1.binSearch(nums, target), o);
EXPECT_EQ(solution2.binSearch(nums, target), o);
}
TEST(Problem704, Case2) {
vector<int> nums{-1, 0, 3, 5, 9, 12};
int target{2};
int o{-1};
Solution1 solution1;
Solution2 solution2;
EXPECT_EQ(solution1.binSearch(nums, target), o);
EXPECT_EQ(solution2.binSearch(nums, target), o);
}