File tree Expand file tree Collapse file tree 3 files changed +57
-1
lines changed
05-Binary-Search-Tree/Course Code (C++)/Optional-03-Lower-Bound-and-Upper-Bound-in-Binary-Search Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.5 )
2
+ project (Optional_03_Lower_Bound_and_Upper_Bound_in_Binary_Search )
3
+
4
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
+
6
+ set (SOURCE_FILES main.cpp )
7
+ add_executable (Optional_03_Lower_Bound_and_Upper_Bound_in_Binary_Search ${SOURCE_FILES} )
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < cassert>
3
+
4
+ using namespace std ;
5
+
6
+ // 二分查找法, 实现lower_bound
7
+ // 即在一个有序数组arr中, 寻找大于等于target的元素的第一个索引
8
+ // 如果存在, 则返回相应的索引index
9
+ // 否则, 返回arr的元素个数 n
10
+ template <typename T>
11
+ int lower_bound (T arr[], int n, T target){
12
+
13
+ assert (n >= 0 );
14
+
15
+ int l = 0 , r = n;
16
+ while (l != r){
17
+ int mid = l + (r - l) / 2 ;
18
+ if (arr[mid] < target)
19
+ l = mid + 1 ;
20
+ else // nums[mid] >= target
21
+ r = mid;
22
+ }
23
+ return l;
24
+ }
25
+
26
+ // 二分查找法, 实现upper_bound
27
+ // 即在一个有序数组arr中, 寻找大于target的元素的第一个索引
28
+ // 如果存在, 则返回相应的索引index
29
+ // 否则, 返回arr的元素个数 n
30
+ template <typename T>
31
+ int upper_bound (T arr[], int n, T target){
32
+
33
+ assert (n >= 0 );
34
+
35
+ int l = 0 , r = n;
36
+ while (l != r){
37
+ int mid = l + (r - l) / 2 ;
38
+ if (arr[mid] <= target)
39
+ l = mid + 1 ;
40
+ else // nums[mid] > target
41
+ r = mid;
42
+ }
43
+ return l;
44
+ }
45
+
46
+ int main () {
47
+
48
+ return 0 ;
49
+ }
Original file line number Diff line number Diff line change 121
121
| 本章课程最终代码 | [ C++源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(C%2B%2B)/Chapter-05-Completed-Code ) | [ Java源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(Java)/Chapter-05-Completed-Code/src/bobo/algo ) |
122
122
| 补充1 二分查找法改变变量定义,论如何写出正确算法 | [ 玩转算法面试] ( http://coding.imooc.com/class/82.html ) | [ 第三章1,2小节] ( http://coding.imooc.com/lesson/82.html#mid=2656 ) |
123
123
| 补充2 二分搜索法的floor和ceil | [ C++源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(C%2B%2B)/Optional-02-Floor-and-Ceil-in-Binary-Search ) | [ Java源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(Java)/Optional-02-Floor-and-Ceil-in-Binary-Search/src/bobo/algo/ ) |
124
- | 补充3 二分搜索法的lower bound和upper bound | [ 整理中 ] | [ 敬请期待] |
124
+ | 补充3 二分搜索法的lower bound和upper bound | [ C++源码 ] ( 05-Binary-Search-Tree/Course%20Code%20(C%2B%2B)/Optional-03-Lower-Bound-and-Upper-Bound-in-Binary-Search/ ) | [ Java源码 ] [ 敬请期待 ] |
125
125
| 补充4 二分搜索的总结 | [ 整理中] | [ 敬请期待] |
126
126
| 补充5 二分搜索树中的floor和ceil | [ C++源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(C%2B%2B)/Optional-05-Floor-and-Ceil-in-BST/ ) | [ Java源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(Java)/Optional-05-Floor-and-Ceil-in-BST/src/bobo/algo/ ) |
127
127
| 补充6 二分搜索树中的前驱和后继 | [ C++源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(C%2B%2B)/Optional-06-Predecessor-and-Successor-in-BST/ ) | [ Java源码] ( https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/05-Binary-Search-Tree/Course%20Code%20(Java)/Optional-06-Predecessor-and-Successor-in-BST/src/bobo/algo/ ) |
You can’t perform that action at this time.
0 commit comments