查找排序数组的插入位置
来源:互联网 发布:淘宝上的古董店 编辑:程序博客网 时间:2024/06/02 01:33
题目:
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
具体代码如下:
int searchInsert(int A[], int n, int target) {
if(n<=0)return 0;
int l=0,r=n-1,m=l+(r-l)/2;
while(l<r)
{
if(A[m]>=target)r=m;
if(A[m]<target)l=m+1;
m=l+(r-l)/2;
}
if(A[m]>=target)
return m;
return m+1;
}
- 查找排序数组的插入位置
- 数组的排序,反转,折半查找,插入
- 数组的排序,反转,折半查找,插入
- 插入排序(顺序数组中找要插入元素的位置)
- 折半查找确定插入数组中元素的位置
- Java数组-二分查找法用于元素插入有序数组,并获取元素插入的位置
- 一个数组 输入一个数 查找到它的位置 插入合适的位置
- 插入元素到有序数组,二分搜索查找插入位置
- 在排序好的数组插入一个数字,数字要插入到合适的位置上
- 数组/排序,插入,查找,平均值,最值
- 【java】输入数组、数组排序及查找数组位置
- 数组的插入排序
- Search Insert Position 查找插入的位置
- 寻找有序数组数组的插入位置
- 35.查找插入位置
- 33.在旋转排序的数组中查找某值位置
- Java 数组的插入排序
- c语言心得-----数组中对元素的操作排序,查找,插入,和删除
- Floyd算法
- FileZilla FTP服务器源代码分析09
- bootstrap fluid affix
- 脚本查看linux 密码更改日期和账户失效日期
- Java随机生成字母
- 查找排序数组的插入位置
- FileZilla FTP服务器源代码分析10
- linux下查看mysql版本
- 正态分布的前世今生(五)
- 剑指offer面试题4 替换一个字符串中的空格为 O(n)算法 和把两个有序数组合起来 依旧有序的算法
- C# WinForm控件随窗体大小改变自动适应改变
- 认识*.so里的JNI_OnLoad()函数
- 2013--腾讯笔试面试经历
- 桌面右键弹出二级菜单