描述

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

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

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

样例

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. CSS3媒体查询的部分重要属性

    width:视口宽度 height:视口高度 device-width:渲染表面的宽度,就是设备屏幕的宽度 device-height:渲染表面的高度,就是设备屏幕的高度 orientation:检查 ...

  2. light1370 欧拉函数打表

    /* 给定n个数ai,要求欧拉函数值大于ai的最小的数bi 求sum{bi} */ #include<bits/stdc++.h> using namespace std; #define ...

  3. cf1110E 思维

    /* Ci'=Ci+1 + Ci-1 -Ci Ci+1 - Ci' = Ci - Ci-1 Ci' - Ci-1 = Ci+1 - Ci; 即求一次Ci’等价于交换Ci和Ci-1 与 Ci+1和Ci的 ...

  4. Oracle下载 OPatch

    今天被朋友问及,如何下载OPatch ...我当时有些凌乱的.事后想想,人与人的思维是不同的,对待同一个问题,有人觉得很简单,有人觉得无从下手 . 乱不多说了.开始说明下吧. 1. 首先要有一个MOS ...

  5. ActiveSync之HTTP

    Exchange ActiveSync:HTTP协议让客户端能够同步存储在服务器上的数据. 互联网工程工作小组(IETF)要求所有互联网协议都比支持UTF-8编码.因此,ActiveSync传输所使用 ...

  6. reactnative的js里的super的作用

    class Blink extends Component{ constructor(props){ super(props); this.state = { showText: true }; se ...

  7. DOM对象,控制HTML元素

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 节点属性: 遍历 ...

  8. VS2015 生成事件 命令参数

    来源:https://stackoverflow.com/questions/11001822/copy-files-from-one-project-to-another-using-post-bu ...

  9. WPF 下两种图片合成或加水印的方式(转载)

    来源:http://www.cnblogs.com/lxblog/ 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名 ...

  10. python---数学表达式的分析树实现

    先走一遍, 前面很多知道点,都串起来了. # coding = utf-8 # 使用列表实现栈的功能 class Stack: def __init__(self): self.items = [] ...