二分查找是一种十分重要的查找算法.通常在有序数组中查找某个指定的数.
从有序数组中找出指定元素第一次出现的位置。
1 | int findLastPos(vector<int>& nums, int target){ |
从有序数组中大于等于某个数的元素第一次出现的位置
1 | int getLessIndex(vector<int> &help, int num) { |
测试用例:
1 | [1, 3, 3, 3, 3] // 1, 元素3(最早出现的元素3) |
从有重复元素的有序数组中找出某个元素第一次出现的位置
1 | int getLessIndex(vector<int> &help, int num) { |
测试用例
1 | [1, 3, 3, 3, 3] // 1, 第一个元素3 |
相关题目
在有序旋转数组中搜索某个给定的数:https://leetcode.com/problems/search-in-rotated-sorted-array
Find peak Element: https://leetcode.com/problems/find-peak-element
- 如何处理一个数组中所有元素都相等的情况.
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/>
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/