题目:

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. Back to Edit Distance(LCS + LIS)

    Given 2 permutations of integers from 1 to N, you need to find the minimum number of operations nece ...

  2. 游戏BUFF设计

    游戏中的BUFF/DEBUFF我们见过很多,我见到的玩得比较泛滥的就属WAR3.魔兽世界.九阴真经.仿DOTA类的如LOL. 总体上来说,BUFF/DEBUFF都属于“临时的技能效果”,因此它们可以沿 ...

  3. C#入门随手笔记

    1..Net Framework是Microsoft为开发应用程序而创建的一个开发平台. 运行操作系统不限:Microsoft版本运行在windows,Mono版本运行开Linux和MacOS: 应用 ...

  4. appid 评价

    //apple api #define kAppAppleId         @"980883989" #define kAppRateUrl         @"it ...

  5. canvas对象arcTo函数的使用-遁地龙卷风

    (-1)环境说明 我使用的浏览器是chrome49 (1)详细介绍 $(function() { var context = lol.getContext("2d"); conte ...

  6. 服务器&域名那些事儿

    购买的阿里云的服务器(ECS)和域名 请移步: 服务器&域名那些事儿 服务器&域名那些事儿2 github 博客

  7. 关于js css html加载顺序整理

    1.js放在head中会立即执行,阻塞后续的资源下载与执行.因为js有可能会修改dom,如果不阻塞后续的资源下载,dom的操作顺序不可控. 正常的网页加载流程是这样的. 浏览器一边下载HTML网页,一 ...

  8. 使用text存储hash类型的数据 Use text filed to store the hash map

    在component表里用text类型的字段存储hash数据 (1)新建字段 ,这是migration的内容 class AddHintsToComponents < ActiveRecord: ...

  9. OpenCV成长之路(3):模仿PhotoShop中魔术棒工具

    本文的主题实际上是图像的颜色空间的转换,借助一个颜色选取程序来说明OpenCV中颜色转换函数的用法以及一些注意事项. 一.几种常见的颜色空间: RGB颜色空间:RGB采用加法混色法,因为它是描述各种“ ...

  10. C#之类的继承、抽象类和虚方法

    代码下载地址 类的继承: 写电池的基类:包含条码和箱体码两个字段,含有两个参数的构造函数 class Battery { public string _barCode; public string _ ...