Search Insert Position
来源:互联网 发布:ambr软件 编辑:程序博客网 时间:2024/06/05 13:24
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it wereinserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7→ 4
[1,3,5,6], 0 → 0
实现代码如下:
*/function findInsertPosition($arr, $target) { $low = 0; $high = count($arr) - 1; if ($high < 1) return false; while ($low <= $high) { $mid = floor(($low + $high) / 2); if ($arr[$mid] == $target) return $mid; else if ($arr[$mid] > $target) $high = $mid - 1; else $low = $mid + 1; } return $low;}$arr = [1,3,5,6];echo findInsertPosition($arr, 7);
阅读全文
0 0
- LeetCode: Search Insert Position
- Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode34:Search Insert Position
- Search Insert Position
- [35]Search Insert Position
- Search Insert Position
- Search Insert Position
- [LeetCode]Search Insert Position
- Search Insert Position
- [LeetCode] Search Insert Position
- Search Insert Position
- Search Insert Position
- wamp上的apache如何配置ssL证书,将我们的http改成Https
- Codeforces 834 C The Meaningless Game(二分)
- Java程序员最常犯的错误盘点
- neo4j 的操作
- 算法学习记录一(C++)--->二维数组中的查找
- Search Insert Position
- 40. Combination Sum II
- Hello,Blogs
- Python 进程内存增长解决方案
- Linux下 crond和crontab
- NodeJS excel 导出(exceljs)
- Android 最火的快速开发框架XUtils
- OpenCv使用BP神经网络将图片分为三类
- 浅析python 中__name__ = '__main__' 的作用