【leetcode】 search Insert Position(middle)
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
思路:
与普通二分搜索的唯一区别在于 可能插入的位置是没找到时 l 的值
int searchInsert(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(A[m] == target)
return m;
else if(A[m] > target)
r = m - ;
else
l = m + ;
}
return l; //注意 就这里不一样 最后r = l - 1 返回l就可以了 就是应该插入的位置
}
【leetcode】 search Insert Position(middle)的更多相关文章
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【题解】【数组】【查找】【Leetcode】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- 【leetcode】Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- Windows Server 2008 R2 IIS7.5下PHP、MySQL快速环境配置【图】
众所周知,win平台的服务器版本默认是不能运行php的,需要对服务器进行环境配置. 而许多朋友纠结如何配置,在百度上搜索出的教程一大堆,基本步骤复杂,新手配置容易出错. 今天,邹颖峥教大家一种快速配置 ...
- [译]git clone
git clone git clone命令copy一个已经存在的Git仓储. git clone有点像svn的checkout, 他的不同之处是这个copy也是一个完整的仓储-它有自己的历史纪录, 能 ...
- linux命令行netstat总结
1.所谓的监听就是某个服务程序会一直常驻在内存中,所以该程序启动的Port就会一直存在. 2.在小于1023的端口,都是需要以root身份才能够启动的. 3.大于1024以上的Port主要是作为cli ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 牡丹江.2014k(构造)
K - Known Notation Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ...
- jQuery对话框插件 ThickBox
http://baike.haosou.com/doc/7607201-7881296.html 项目已经停止维护,但该插件还是不错的! ThickBox是一个基于JQuery类库的扩展,它能在浏览器 ...
- codemirror和ace editor的语法高亮
两个javascript库用做在线代码编辑器都是非常优秀的选择 我这两天对这两个类库做了简单的研究,重点是语法高亮的自定义: ace editor的主要思路是生成状态机,从一个startstate开始 ...
- 跟着百度学PHP[4]OOP面对对象编程-13-魔术方法__set(),__get(),__isset(),__unset()
__set() 在对象访问私有成员的时候自动被调用,达到了给你看,但是不能给你修改的效果!(在对象访问一个私有的成员的时候就会自动的调用该魔术方法) __get() 方法用于获取私有属性值.(在设置私 ...
- PHP中interface与 implements 关键字
类中接口的应用 1.关键字:interface 2.关键字:implements 1.接口的介绍与创建 接口:一种成员属性全部为抽象或常量的特殊抽象类. 规则: 1.类中全部为抽象方法. 2.抽象方法 ...
- Android 内容提供器(Content Provider)介绍
内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性.目前,使用内容 ...