LeetCode Search Insert Position
来源:互联网 发布:js button disabled 编辑:程序博客网 时间:2024/05/17 06:02
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 were inserted 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
class Solution {public:int searchInsert(int A[], int n, int target) {return biSearch(A, 0, n - 1, target);}int biSearch(int A[], int L, int R, int target){if (R < L)return L; //注意返回的 是L, 代表应该插入的位置int mid = L + ((R - L) >> 1);if (A[mid] == target)return mid;else if (A[mid] > target)return biSearch(A, L, mid - 1, target);elsereturn biSearch(A, mid + 1, R, target);}};
0 0
- LeetCode: Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- [LeetCode]Search Insert Position
- [LeetCode] Search Insert Position
- LeetCode-Search Insert Position
- LeetCode - Search Insert Position
- LeetCode | Search Insert Position
- Leetcode: Search Insert Position
- 【LeetCode】Search Insert Position
- Leetcode Search Insert Position
- Leetcode: Search Insert Position
- LeetCode:Search Insert Position
- leetcode Search Insert Position
- LeetCode Search Insert Position
- 黑马程序员--选择排序、冒泡排序、二分法查找
- 编码风格
- Java 并发编程__内存模型、线程同步机制
- hdu 1856 More is better
- 如何配置davinci内存
- LeetCode Search Insert Position
- linux-echo服务器
- .NET设计模式之(单例模式)
- HDOJ 题目2208唉,可爱的小朋友(搜索,dfs)
- 逐层打印二叉树节点数据
- POJ 2418 Hardwood Species
- 访问phpmyadmin出现空白界面
- c# 容器的双缓冲
- 算法题:求数组中最小的k个数