题目:

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. php面向对象面试题

    php面试题之四--PHP面向对象(基础部分) 四.PHP面向对象 1. 写出 php 的 public.protected.private 三种访问控制模式的区别(新浪网技术部) public:公有 ...

  2. 免费的Android UI库及组件推荐

    短短数年时间Android平台就已经形成了一个庞大而活跃的开发者社区.许多社区开发的项目业已进入成熟阶段,甚至可以用于商业的软件生产中,且不用担心质量问题. 本文编译自androiduipattern ...

  3. iOS开发——UI基础-按钮的创建和设置

    @interface ViewController () - (IBAction)customBtnClick; @end @implementation ViewController - (void ...

  4. hdu4747——Mex

    1.题目大意:对一个序列的每一个区间求Mex,最后所有的mex相加(mex就是SG的那个),力求nlogn... 2.分析:最近开始刷线段树了,还是有很多不会啊 首先把1-1 1-2 1-- 1-n这 ...

  5. NGUI Sprite 和 Label 改变Layer 或父物体后 未更新深度问题

    using UnityEngine; using System.Collections.Generic; /// <summary> /// Sprite is a textured el ...

  6. curl模拟自动登陆&采集网页数据

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  7. EXT Grid celleditor列编辑,动态控制某一单元格只读

    listeners : { beforeedit:function(editor, context, eOpts) { if(context.record.data.hasRatio == " ...

  8. 越狱后的ios如何用apt-get 安装各种命令

    越狱后的ios如何用apt-get 安装各种命令   iphone越狱后想玩linux. 1. ssh 客户端:ssh Term Pro. 2. 只装客户端是连不上的,还得一个 ssh connect ...

  9. C# 毕业证书打印《二》

    当证书的打印功能得以实现,最关键的功能便是数据. 通过对打印的了解,打印中最关键的功能便是打印事件中的方法. private void pd_PrintPage(object sender, Prin ...

  10. 【转】mysql的cardinality异常,导致索引不可用

    转自:http://ourmysql.com/archives/1343 前段时间,一大早上,就收到报警,警告php-fpm进程的数量超过阈值.最终发现是一条sql没用到索引,导致执行数据库查询慢了, ...