在给定的有序数组中插入一个目标数字,求出插入该数字的下标
由于该数组是已经排好序的数组,可以利用二分查找。
 
二分查找的返回结果:
  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. 【Intel AF 2.1 学习笔记一】AF程序结构

    Intel App Framework(原jqMobi)是用来开发hybrid app的开源免费框架,被intel收编之后发布了最新的2.1版本,最近正在学习.af的所谓程序结构,就是AF网页的架构, ...

  2. jsch下载文件的两个注意点

    1.关于sftp文件上传和下载的网上很多code,此处就不多写了.主要记录下工作中遇到的两个问题需要多注意. 1.判断sftp远程文件是否存在,通过异常捕获来判断该文件是否存在,存在返回SftpATT ...

  3. VS2008编译错误:error C2065: 'PMIB_TCPSTATS' : undeclared identifier c:\program files (x86)\microsoft sdks\windows\v7.0a\include\iphlpapi.h 411

    安装了VS2008编译之前的程序,结果出现了编译错误,以为是VS2008的Sp1补丁没装好,重装补丁后还是不行,编译错误如下: 双击错误会定位在iphlpapi.h中, 一个可行的解决办法是:把iph ...

  4. thinkphp 在阿里云上的nginx.config配置

    # For more information on configuration, see: # * Official English Documentation: http://nginx.org/e ...

  5. 解决 iOS7 通过tag 找不到 UITableViewCell 的子控件(转)

    转自:http://www.cnblogs.com/waiwaibuzhidao/p/3340400.html 当iOS7问世,程序的世界就混乱了,以前良好的程序,现在是一塌糊涂,于是只能把问题一个一 ...

  6. nuget类库xml说明以及类库说明文件添加到包中

    1.nuget包制作添加xml操作:项目右键属性,生成配置输出xml文档文件,debug,release都配置一下,项目右键 yesway.redis.csproj 文件增加: 添加类库说明文件con ...

  7. 架构设计:系统存储(28)——分布式文件系统Ceph(挂载)

    (接上文<架构设计:系统存储(27)--分布式文件系统Ceph(安装)>) 3. 连接到Ceph系统 3-1. 连接客户端 完毕Ceph文件系统的创建过程后.就能够让客户端连接过去. Ce ...

  8. web -- 前端访问后台跨区问题解决

    package com.xindatai.ibs.web.filter; import java.io.IOException; import javax.servlet.Filter; import ...

  9. SpringMVC -- 梗概--源码--壹--跳转

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  10. 本地Chrome测试JS代码报错:XMLHttpRequest cannot load

    这种file跨域问题在火狐下是不存在的 解决Chrome下file跨域问题: 在Chrome应用程序下,右键属性,目标处添加"--allow-file-access-from-files&q ...