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. [翻译]:怎样从C/C++代码中对C#进行回调

    声明:网络上类似的中文博客大有存在,本人知识水平有限,业余爱好,也是为了备份收藏How to make a callback to C# from C/C++ code 本着共享知识的初衷,翻译一份给 ...

  2. IP首部校验和计算

    根据RFC1071文档的计算方法,编写代码实现IP首部校验和的计算 计算步骤: 1.首先将IP首部中校验和字段置0 2.将IP首部每16bit进行相加,如果有进位产生,则将进位加到最低位. 3.将计算 ...

  3. (转)ant 使用指南

    ant 使用指南  文件转载自:http://www.cnblogs.com/hoojo/archive/2013/06/14/java_ant_project_target_task_run.htm ...

  4. C#发送邮件

    以下代码已用 .yeah .qq 以及本人公司邮箱测试通过,可多发,可挂附件 本次测试邮件发送类,是用的winform 页面如下 窗口的后台代码如下: using System; using Syst ...

  5. MySQL——保证数据的完整性

    为了防止垃圾的产生,从而影响数据库的执行效率. 1实体完整性——行数据的有效性   唯一约束(unique).主键约束(primary key) 2域完整性——列数据的有效性 非空约束(not nul ...

  6. Java多线程开发技巧

    很多开发者谈到Java多线程开发,仅仅停留在new Thread(...).start()或直接使用Executor框架这个层面,对于线程的管理和控制却不够深入,通过读<Java并发编程实践&g ...

  7. HTML5中canvas大小调整

    今天用到canvas元素,发现它的大小不是像普通dom元素一样,直接设置css样式可以改变的,它会由自己原本的大小伸缩. 例如, <canvas id='canvas'></canv ...

  8. Quartz.Net与MVC结合定时任务

    1.首先,我们打开Visual Studio 2015,创建一个ASP.NET MVC的Web应用程序项目. 2.然后通过程序包管理器控制台来安装Quartz.Net组件. Quartz.Net一个最 ...

  9. 【Java】JavaWeb权限管理

    权限管理分析 每个网站都涉及到访问权限的控制.每个站点资源都需要被管理起来,用户只有具有访问某个资源的特定权限,才能够访问,否则拒绝访问.网站的访问权限控制,一种方法从 URI 入手,站点的每个资源都 ...

  10. The Strategy pattern

    public class Strategy {public static void main(String[] args) {int [] array=new int[]{26,25,15,42,36 ...