在给定的有序数组中插入一个目标数字,求出插入该数字的下标
由于该数组是已经排好序的数组,可以利用二分查找。
 
二分查找的返回结果:
  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. C# 在多线程环境中,进行安全遍历操作

    本文以List作为操作对象MSDN官方给出的List的线程安全的说法:此类型的公共静态成员是线程安全的.但不能保证任何实例成员是线程安全的.只要不修改该集合,List 就可以同时支持多个阅读器.通过集 ...

  2. C# 抓取网页的img src带参数的图片链接,并下载

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. [mysql] mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in

    From: http://www.ttlsa.com/php/deprecated-mysql-connect/ php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5 ...

  4. Yii2 响应部分 response

    当应用完成处理一个请求后, 会生成一个yii\web\Response响应对象并发送给终端用户 响应对象包含的信息有HTTP状态码,HTTP头和主体内容等, 网页应用开发的最终目的本质上就是根据不同的 ...

  5. CALayer: autoresizingMask

    UIView 可以设定 autoresizingMask,当它的 superView 尺寸改变时,适应何种变化. 不过 CALayer 却没有这个属性,如和做到让 CALayer 和 UIView 一 ...

  6. C++ 著名程序库 概览

          本文转载自: http://ace.acejoy.com/thread-3777-1-1.html   1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库B ...

  7. 最新Java校招面试题及答案

    本文作者在一年之内参加过多场面试,应聘岗位均为 Java 开发方向.在不断的面试中,分类总结了 Java 开发岗位面试中的一些知识点. 主要包括以下几个部分: Java 基础知识点 Java 常见集合 ...

  8. BarTender条码检验位类型知识讲解

    检验位类型指定BarTender使用哪一种算法来计算符号的附加检验位.使用“BarTender检验位类型”选项可以从您的符号所支持的检验位类型中选择一种检验位类型.下面,小编就给大家分享设置检验位类型 ...

  9. PHP 数组current和next用法

    1.current   当前数组 <?php $transport = array('foot', 'bike', 'car', 'plane'); $mode = current($trans ...

  10. python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)

    介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...