在给定的有序数组中插入一个目标数字,求出插入该数字的下标
由于该数组是已经排好序的数组,可以利用二分查找。
 
二分查找的返回结果:
  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. unity3d 读取usb摄像头

    using UnityEngine; using System.Collections; public class C : MonoBehaviour { private WebCamTexture ...

  2. useradd groupadd passwd usermod userdel chfn id

    chfn   修改用户信息 id 显示当前用户和用户组的id

  3. Oracle12c 在 Ubuntu 12.04 ~ 18.04 的安装注意事项

    必须的注意点: 1:/bin/sh 必须指向 bash or ksh 2:/usr/lib64 可以忽略的事情: 1:gcc 版本无所谓 2:libstdc++5 无需安装 3:libaio 版本无所 ...

  4. C#文件操作工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. windows2008 使用nginx 反向代理实现负载均衡解决HTTPS 证书问题

    由于项目需要 负载均衡由NBL 转成nginx 反向代理.考虑都是https模块,所以证书成了个难题. 解决方案: 1.下载openssl(windows 安装包) 2.打开bin/下面的openss ...

  6. mysql数据库2

    命令行客户端软件MySQL Command Line Client, 打开该程序,输入数据库密码,登陆到MySQL软件, 如果想通过该命令行工具来操作MySQL软件,只需要在"mysql&g ...

  7. 如何使用 MasterPage

    MasterPageFile母版页 刚开始学,什么都不懂,看到了这段代码,才促使自己去研究MasterPageFile到底是什么含义.<%@ Page Language="C#&quo ...

  8. apache 配置会话保持

    1.修改apache_home/conf/httpd.conf,增加以下模块(取消注释,如有其他依赖, 则相应取消注释) LoadModule proxy_module modules/mod_pro ...

  9. python concurrent.futures包使用,捕获异常

    concurrent.futures的ThreadPoolExecutor类暴露的api很好用,threading模块抹油提供官方的线程池.和另外一个第三方threadpool包相比,这个可以非阻塞的 ...

  10. [原]NGUI之按钮置灰

    传统按钮置灰,需要使用另外一张纹理. 本例通过修改shader和NGUI sprite的r值实现按钮置灰.优势:节省纹理,操作简单 将NGUI Unlit/Transparent Colored片段部 ...