题目:

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

代码:

第一反应,遍历了一遍:

public int searchInsert(int[] nums, int target) {
   int result =0;
   for (int i = 0 ; i < nums.length ; i++)
   {
    if (target <= nums[i])
     {
      result = i;
      break;
     }
    else
    {
     result=i+1;
    }
   }
      return result;
     }

网上查了一下,发现二分法,算法复杂度会减少不少,尤其在较大的数据量:

int searchInsert_mid(int[] nums, int target) {
   int low = 0, high = nums.length-1;
   while(low <= high) {
    int mid = (low+high)>>1;
    if(nums[mid] == target)
     return mid;
    else if(nums[mid] > target)
     high = mid-1;
    else
     low = mid+1;
   }
   if(high < 0) return 0;
   if(low >= nums.length) return nums.length;
   return low;
  }

35. Search Insert Position的更多相关文章

  1. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  2. [array] leetcode - 35. Search Insert Position - Easy

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

  3. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  4. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  5. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  6. 35. Search Insert Position@python

    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 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

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

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

  10. 【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#5.0 特性

    Visual Studio 2012 中 Visual C# 的新增功能 Lambda表达式 表达式树:把代码,转换成数据,然后分析数据发现其组成部分,最后转换成可以传递到其他程序的字符串 LinQ表 ...

  2. 3_mysql 主从复制

    mysql 主从复制 网易数据库 石勇 提纲 什么是主从复制 主从复制的原理 主从复制的用途 主从复制的搭建 主从复制的问题 什么是主从复制 数据拷贝 准实时 源-主节点:目的-从节点 主从复制的原理 ...

  3. java 在接口里函数不能重载?

    项目里使用hession远程调用serviceRemote接口,而serviceRemote接口里有两个save方法,参数分别是CpCredential对象和List,但运行发现会报莫名其妙的错. 后 ...

  4. 原生态js获取节点的方法

    <input value="我是用id来获取值的" type="button" onclick="GetById()"/> &l ...

  5. 数据源和JNDI的关系:

    DataSource对象是由Tomcat提供的,因此不能在程序中采用创建一个实例的方式来生产DataSource对象,而需要采用Java的另一个技术JNDI,来获得DataSource对象的引用. T ...

  6. Linux之Shell脚本计算命令行的所有和

    问题:设计一个Shell脚本:求命令行上所有整数和 Shell代码: 调用add.sh脚本结果:

  7. centos终端中mysql中文显示乱码的处理

    http://stackoverflow.com/questions/3513773/change-mysql-default-character-set-to-utf-8-in-my-cnfhttp ...

  8. EXTJS 6 必填项加星号*

    /**重写ext filed组件, 实现表单必填项加红色*星号**/ Ext.override(Ext.form.field.Base,{ initComponent:function(){ if(t ...

  9. C++数学、信号处理相关库

    1.Eigen 是一个线性算术的C++模板库,包括:vectors, matrices, 以及相关算法.功能强大.快速.优雅以及支持多平台. http://www.oschina.net/p/arma ...

  10. shell脚本实现拷贝大文件显示百分比的代码分享

    #!/bin/sh strace -q -eread cp -- "${1}" "${2}" 2>&1 \| awk '{    count += ...