Array Binary Search

Description:

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

my Solution:

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

Best Solution:

public int searchInsert(int[] A, int target) {
int low = 0, high = A.length-1;
while(low<=high){
int mid = (low+high)/2;
if(A[mid] == target) return mid;
else if(A[mid] > target) high = mid-1;
else low = mid+1;
}
return low;
}

差别就在于,我用的是从首到尾循环,没有完全利用好已排序这个条件。最优解用的是二分法,基本是排序里的算法。

LeetCode & Q35-Search Insert Position-Easy的更多相关文章

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

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

  2. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  3. LeetCode--Array--Remove Element && Search Insert Position(Easy)

    27. Remove Element (Easy)# 2019.7.7 Given an array nums and a value val, remove all instances of tha ...

  4. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

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

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

  7. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  8. 【题解】【数组】【查找】【Leetcode】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 ...

  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. 微信公众号接口配置验证URL

    /** * 审核填写的URL */ public function checkUrl(){ //获得参数 signature nonce token timestamp echostr $nonce ...

  2. [翻译]简单的实现一个Promise

    英文原文为:https://www.promisejs.org/implementing/ 1. 状态机 因为 promise 对象是一个状态机,所以我们首先应该定义将要用到的状态. var PEND ...

  3. office 2013补丁包更新

    总是有客户发现使用office 2013 下的插件有问题,这个问题出现在低版本上,所以要给office 2013打上补丁,打上后,运行插件ok,出现的bug解决掉了.那么给office打补丁包就成了解 ...

  4. git仓库搭建及客户端使用

    这里只在linux上做git仓库搭建 这里只在linux上做git仓库搭建 这里只在linux上做git仓库搭建 linux 服务器上安装及配置git 一.安装git yum install -y g ...

  5. 使用Jmeter自带的 Http 代理服务器录制脚本

    最近要测试某个模块的压力测试,所以使用Jmeter录制脚本 1.       打开JMeter工具 创建一个线程组(右键点击“测试计划”--->“添加”---->“线程组”) 创建一个ht ...

  6. phpstorm激活码生成器地址

    http://idea.qinxi1992.cn/ 写在windows/system32/drivers/etc/hosts里 0.0.0.0 account.jetbrains.com 激活码位置: ...

  7. tp框架的url模式

    tp框架url地址可以由以下四种 http://网址/index.php?m=XX&c=XX&a=XX   基本get模式 http://网址/index.php/模块/控制器/操作方 ...

  8. Cesium 获取鼠标当前位置的模型高度,地形高度,OSGB高度,及其经纬度。

    var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene._imageryLayerCollection);var ray,posit ...

  9. Eclipse项目出现红色叹号的解决办法

    以前的项目今天打开突然出现了红色的叹号,对于强迫症的患者简直忍不了,出现红色叹号的原因都是jar包出现问题导致的,如果是代码错误早就是一个大红叉了- 打开项目就可以发现,找不到哪里出问题了,代码和js ...

  10. Linux快速搭建FTP服务器

    FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制文件的双向传输.同时,它也是一个应用程序(A ...