在给定的有序数组中插入一个目标数字,求出插入该数字的下标
由于该数组是已经排好序的数组,可以利用二分查找。
 
二分查找的返回结果:
  1. 当查找的数字在数组中时,返回第一次出现的下标
      2. 当查找的数字不存在时,返回 - pos - 1(即 应当插入位置的相反数再减去 
 
 
参考代码: 
package leetcode_50;

import java.util.Arrays;

/***
*
* @author pengfei_zheng
* 找出插入元素应当插入的下标
*/
public class Solution35 {
public static int searchInsert(int[] nums, int target) {
int ans = Arrays.binarySearch(nums, target);
if(ans>=0)
return ans;
else
return -ans-1;
}
public static void main(String[]args){
int []nums={1,3,5,6};
System.out.println(searchInsert(nums,0));
} }
 

LeetCode 35 Search Insert Position(查找插入位置)的更多相关文章

  1. [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 ...

  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. [array] leetcode - 35. Search Insert Position - Easy

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

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

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

  5. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  6. Search insert position, 查找插入位置

    问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...

  7. [leetcode 35] Search Insert Position

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

  8. [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 ...

  9. 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 ...

随机推荐

  1. 使用阿里云的maven私服的setting.xml, 提高maven项目jar下载速度

    下载: http://files.cnblogs.com/files/007sx/settings.zip 然后替换自己原本maven的配置文件. 如下载失败,可内容替换: <?xml vers ...

  2. Centos yum安装java jdk1.8

    yum -y install java-1.8.0-openjdk* 安装后 java -version查看版本 检验是否安装成功. 其安装位置 /usr/lib/jvm/java-1.8.0-ope ...

  3. 性能分析Linux服务器CPU利用率

    CPU度量 1.  指标范围 1.1  User mode CPU utilization+ System mode CPU utilization 合理值:60-85%,如果在一个多用户系统中us+ ...

  4. localstorage和sessionstorage上手使用记录

    通过阅读各路大神对web存储locastorage和sessionstorage的用法解析,自己试用了一下,在此留个备忘. 在项目中,如果用到很多次storage,要存储很多数据,就要把它封装成函数了 ...

  5. Unity判断网络是否连接以及判断是否连接WiFi

    由于项目中的核心模块需要用到网络连接,所以需要首先检测用户是否有网络百度了下,有人说通过连接自己的服务器进行测试的,也有人说通过延迟来判断的最后发现原来Unity是提供了网络判断的方法的.Networ ...

  6. JVM垃圾回收机制之引用类型

    一:引用的类型 javac编译器编译源文件后,生成字节码文件,在类加载器加载字节码文件到内存中时,在内存中开辟 空间,栈.堆以及方法区,来存放对象以及引用.引用可以分为四种: 强引用:平常我们在编写程 ...

  7. Do you want a timeout?

    Do you want a timeout?   You’re feeling accomplished and excited; the new features for your applicat ...

  8. Go工具和调试详解

    https://blog.csdn.net/happyanger6/article/details/78724594/ https://blog.csdn.net/u012210379/article ...

  9. go的精选类库

    https://github.com/avelino/awesome-go https://gitee.com/snail/proxy

  10. Gibbs采样

    (学习这部分内容大约需要50分钟) 摘要 Gibbs采样是一种马尔科夫连蒙特卡洛(Markov Chain Monte Carlo, MCMC)算法, 其中每个随机变量从给定剩余变量的条件分布迭代地重 ...