35. Search Insert Position

时间:2022-05-08 02:58:23

题目:

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. &lbrack;Leetcode&rsqb;&lbrack;Python&rsqb;35&colon; Search Insert Position

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

  2. &lbrack;array&rsqb; leetcode - 35&period; Search Insert Position - Easy

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

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

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

  4. LeetCode练题——35&period; Search Insert Position

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

  5. 【LeetCode】35&period; Search Insert Position &lpar;2 solutions&rpar;

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

  6. 35&period; Search Insert Position&commat;python

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

  7. &lbrack;LeetCode&rsqb; 35&period; Search Insert Position 搜索插入位置

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

  8. &lbrack;leetcode 35&rsqb; Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  9. &lbrack;LeetCode&rsqb; 35&period; 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题意分析&amp&semi;解答】35&period; Search Insert Position

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

随机推荐

  1. DELPHI实现百度开放平台

    学习百度语音 百度语音开发平台 http://yuyin.baidu.com/ 百度开发者账号 13600514494,短信登录 应用账号 词语听写 App ID: 7799366 API Key: ...

  2. 用Canvas制作loading动画

    上一篇讲到用SVG制作loading动画,其中提到了线性渐变在扇形区域中的问题,并且用SVG SIML语法制作的loading动画并不是所有浏览器都兼容,所以现在用Canvas重新实现了一遍. 这里与 ...

  3. &lowbar;cpluscplus

    _cpluscplus是c++中的定义,而c中没有该定义 1.用来判定代码是c类型还是c++类型 2._cplusplus的类型是"long int",值为199711L int ...

  4. CSRF攻击&lbrack;转&rsqb;

    一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...

  5. poj1436 Horizontally Visible Segments

    这是一个区间更新的题目,先将区间放大两倍,至于为什么要放大可以这样解释,按照从左到右有4个区间,y值是[1,5],[1,2],[3,4],[1,4]如果不放大的话,查询[1,4]区间和前面区间的”可见 ...

  6. 在eclipse中安装activiti插件

    最近在学习activiti,先学习安装插件吧. 单击help->Install new Software 然后添加资源 name:activiti location:http://activit ...

  7. oracle 命令创建用户 、授权、数据库导入、导出

    最近在使用oracle,经常要导入导出数据,命令很简单,却经常忘记,所以记下来.. drop user yfplss cascade;--登录system用户删除已存在的用户名,该用户下的所有东西都被 ...

  8. JavaScript的DOM编程--05--获取文本节点

    获取文本节点: 1). 步骤: 元素节点 --> 获取元素节点的子节点 2). 若元素节点只有文本节点一个子节点, 例如 <li id="bj" name=" ...

  9. MSYS 编译 nginx rtmp-module

    1. 下载源码 http://hg.nginx.org/nginx nginx-c74904a17021.zip https://github.com/arut/nginx-rtmp-module n ...

  10. A 暴力搜索 剪枝是关键

    Description 盖伦是个小学一年级的学生,在一次数学课的时候,老师给他们出了一个难题:老师给了一个正整数 n,需要在不大于n的范围内选择三个正整数(可以是相同的),使它们三个的最小公倍数尽可能 ...