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. command line

    command line terminal vim 编辑工具 vim 编辑命令 j 光标上移 k 光标下移 l 光标左移 h 光标右移 x / dd 删除一行 v 多行模式 :w 保存 :q 不保存退 ...

  2. Hibernate QBC运算符

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <> Restrictions.ne() 不等于not   equal > Restrict ...

  3. 23种oop设计模式定义

    创建型模式 单例模式:确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实现.   工厂模式:定义一个用于创建对象的接口,让子类决定将哪一个类实例化.工厂方法使一个类的实例化延迟到子类. 抽象 ...

  4. CSS布局(一)

    本节内容: 没有布局 display属性 margin:auto; max-width 盒模型 没有布局 如果想把所有内容都塞进一栏里,那么不用设置任何布局也是可以的.然而,如果用户把浏览器窗口调整的 ...

  5. 2-5. Working with Compile Time Constants

    #include <array> #include <cstdint> #include <iostream> class MyClass { private: u ...

  6. java基于socket公共聊天室的实现

    项目:一个公共聊天室功能的实现,实现了登录聊天,保存聊天记录等功能. 一.实现代码 1.客户端 ChatClient.java import java.io.BufferedReader; impor ...

  7. 开始研究tigase和android客户端的实现

    2015.7.5. 昨晚总算把tigaase7.0的开发环境搭起来了.稍微看了下代码结构. 主要是auth,db,compnent,cluster,server,xmpp,这几块,准备先发点时间看看开 ...

  8. 复制mueclipse项目到eclipse

    本文适用于将MyEclipse上的项目projectA检出后重命名为projectB的情况,如果只是检出projectA到Eclipse,也可以部分参考 1.从svn上检出Myeclipse项目到Ec ...

  9. mysql中left join ,right join 以及inner join 比较

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  10. pll及其modesim仿真

    100mhz输入时钟,pll层40mhz和200zhm 进入quartus,建立工程,新建图形文件,导入pll模块,设定pll相关参数.   完成pll模块的建立,并生成pll.v文件 建立modes ...