1 题目:

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

2 思路:

变相的二分搜索

注意处理边界和溢出情况

3 代码:

    public int searchInsert(int[] nums, int target) {
// pre handle
if(nums[0] > target){
return 0;
}
if(nums[nums.length-1] < target){
return nums.length;
} /* again, binary search */
int pre = 0;
int after = nums.length-1;
while(pre<=after){
int mid = pre + ((after-pre)>>1);
if(nums[mid]==target){
return mid;
}else if(nums[mid]<target){
if(mid < nums.length-1 && nums[mid+1]>target){
return mid+1;
}
pre = mid+1;
}else if (nums[mid]>target){
if(mid>0 && nums[mid-1]<target){
return mid;
}
after = mid-1;
}
} // will never reach here
return 0;
}

[leetcode 35] Search Insert Position的更多相关文章

  1. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  2. [LeetCode] 35. Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  3. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  4. [LeetCode] 35. Search Insert Position 解决思路

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. LeetCode 35. Search Insert Position (搜索嵌入的位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. [leetcode]35. Search Insert Position寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  8. [LeetCode] 35. Search Insert Position ☆(丢失的数字)

    转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html    思路 Given a sorted array ...

  9. Java [leetcode 35]Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

随机推荐

  1. Nginx-->基础-->排错-->nginx错误总结

    一.启动时错误 1.错误提示: 2016/11/16 17:36:41 [emerg] 2458#2458: getpwnam("nginx") failed 查看错误日志文件内容 ...

  2. SQL Server 数据库 'xxx' 正处于转换状态。请稍后再尝试该语句。

    问题是这样的,最近因义务需要,公司更换了数据库服务器.数据库随之切换到新的服务器上. 服务器是 Windows Server 2012系统,数据库是SQL Server 2012 .上面有 多个数据库 ...

  3. getElementByName()和getElementById的区别

    因为在属性中,id时唯一的,getElementById取出的是一个元素但是可以出现相同的name,取到的是一个Array ,getElementsByName取出的是数组 记录代码如下: <! ...

  4. sql 中 in与exists的对比

    1.exists只能用于子查询,可以替代IN,如果查询到结果则退出内部查询,并将条件标记为TRUE,传回全部结果资料 in 不管匹配到匹配不到,都全部匹配 2.根据上面的解释可以得出结论:如果子查询结 ...

  5. 搭建高可用mongodb集群(三)—— 深入副本集内部机制

    在上一篇文章<搭建高可用mongodb集群(二)—— 副本集> 介绍了副本集的配置,这篇文章深入研究一下副本集的内部机制.还是带着副本集的问题来看吧! 副本集故障转移,主节点是如何选举的? ...

  6. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

  7. SVN常用问题汇总

    参考文档.http://www.cnblogs.com/newsea/archive/2012/04/28/2474818.html 1.客户端取消记住用户名和密码. 2.汉化后设置为中文

  8. Beyond Compare 2

    Beyond Compare 2 确实很好用,差异行不交叉,自动留出空白,比windiff要清楚.

  9. Unity随记

    //切换场景时怎么能让音乐不停? /////////////////////////////////////////////////////////////////// //切换场景时怎么防止某个物体 ...

  10. <%%>标签 什么意思

    <%%>是说这里面的文本不是普通直接输出到客户端的文本,而是需要服务器来解释的.不光是可以写JAVA脚本,这要看具体服务器端使用的是什么技术ASP/JSP/PHP/ASP.NET都使用相同 ...