描述

给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。

你可以假设在数组中无重复元素。

您在真实的面试中是否遇到过这个题?

样例

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.
[1,3,5,6],5 → 2

[1,3,5,6],2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6],0 → 0

挑战

O(log(n)) time

public class Solution {
/**
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: An integer
*/
public int searchInsert(int[] A, int target) {
// write your code here
if(A.length == 0 ){
return 0;
}
if(target < A[0]){
return 0;
}
if(target > A[A.length-1]){
return A.length;
}
int left = 0;
int right = A.length-1; while(left + 1 < right){
int mid = left + (right-left)/2;
if(A[mid] == target){
return mid;
}else if (A[mid] > target){
right = mid - 1;
} else if(A[mid] < target){
left = mid + 1;
} }
//重点
//三种情况:
if(A[left] >= target) {
return left;
}
if(target<= A[right] && target > A[left]) {
return right;
} return right+1; }
}

60.Search Insert Position.md的更多相关文章

  1. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  2. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

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

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

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

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

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

  5. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

  6. 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导致的溢 ...

  7. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

  8. LeetCode: Search Insert Position 解题报告

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

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

随机推荐

  1. 1705: 小明在工作(zzuli)

    题目描述 小明的工作是负责记录饭堂中正在排队的人的信息 在他的工作中会有三种可能的事件发生:     1.编号为id的学生加入到队伍的最后面     2.排在最前面的学生打完饭离开了队伍     3. ...

  2. 【linux】ftp使用端口转发问题

    相关资料: 1.[ssh]端口转发 2.[ftp]主动模式和被动模式 先说结论:用端口转发无法解决ftp客户端与服务器的连接问题,原因是ftp的data端口不固定,不能把所有>1024的端口都做 ...

  3. kali linux 安装wps office

    1.下载wps for linux 版本 wget http://kdl.cc.ksosoft.com/wps-community/download/6757/wps-office_10.1.0.67 ...

  4. hdu4276 依赖背包

    网上题解都是用spfa求1-n路径的,但其实dfs一次就可以了.. #include <iostream> #include <cstdio> #include <str ...

  5. 论文阅读笔记三十四:DSSD: Deconvolutiona lSingle Shot Detector(CVPR2017)

    论文源址:https://arxiv.org/abs/1701.06659 开源代码:https://github.com/MTCloudVision/mxnet-dssd 摘要 DSSD主要是向目标 ...

  6. node.js(一)

    安装官网: https://nodejs.org/en/ 运行代码: var http=require('http') http.createServer(function(req,res){ res ...

  7. org.apache.thrift.transport.TTransportException: Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083.

    1.启动hive的过程中,[hadoop@slaver1 soft]$ hive --service metastore &错误如下所示: 原因:之前启动hive失败了,但是进程以及启动起来, ...

  8. [转] getBoundingClientRect判断元素是否可见

    getBoundingClientRect介绍 getBoundingClientRect获取元素位置 getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视 ...

  9. WebApi 得到提交过来的 post 数据

    byte[] byts = new byte[System.Web.HttpContext.Current.Request.InputStream.Length]; System.Web.HttpCo ...

  10. 【Android】ContentValues的用法

    ContentValues 和HashTable类似都是一种存储的机制 但是两者最大的区别就在于,contenvalues只能存储基本类型的数据,像string,int之类的,不能存储对象这种东西,而 ...