Question

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

Solution 1 -- Naive

Iterate the array and compare target with ith and (i+1)th element. Time complexity O(n).

 public class Solution {
public int searchInsert(int[] nums, int target) {
if(nums==null) return 0;
if(target <= nums[0]) return 0;
for(int i=0; i<nums.length-1; i++){
if(target > nums[i] && target <= nums[i+1]){
return i+1;
}
}
return nums.length;
}
}

Solution 2 -- Binary Search

If the target number doesn't exist in original array, then after iteration, it must be pointed by low pointer.

Time complexity O(log(n))

 public class Solution {
public int searchInsert(int[] nums, int target) {
if (nums == null || nums.length == 0)
return 0;
int start = 0, end = nums.length - 1, mid = (end - start) / 2 + start;
while (start <= end) {
mid = (end - start) / 2 + start;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
start = mid + 1;
else
end = mid - 1;
}
return start;
}
}

Search Insert Position 解答的更多相关文章

  1. 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 ...

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

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

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

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

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

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

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

  6. leetcode-algorithms-35 Search Insert Position

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

  7. LeetCode: Search Insert Position 解题报告

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

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

  9. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

随机推荐

  1. linux中patch命令 -p 选项

    patch命令和diff命令是linux打补丁的成对命令,diff 负责生产xxxxx.patch文件,patch命令负责将补丁打到要修改的源码上.但是patch命令的参数-p很容易使人迷惑,因为对- ...

  2. hdu3410-Passing the Message(RMQ,感觉我写的有点多此一举。。。其实可以用单调栈)

    What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...

  3. 利用智能手机(Android)追踪一块磁铁(三)

    更新磁铁追踪算法的源代码,Android Studio项目工程 github地址:https://github.com/amazingyyc/MagnetLocate 说明:将磁铁的位置信息封装成消息 ...

  4. hdu 5611 Baby Ming and phone number(模拟)

    Problem Description Baby Ming collected lots of cell phone numbers, and he wants to sell them for mo ...

  5. ACM—Number Sequence(HDOJ1005)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: A number sequence is defined as follows: f ...

  6. python3-day1(文件操作)

    index: str.fomat() open file str.replace 一.新款str.fomat() 1.>>> '12'.zfill(5) '00012' 2.> ...

  7. Python模块学习笔记— —time与datatime

    Python提供了多个内置模块用于操作日期时间.像calendar,time,datetime.首先对time模块中最经常使用的几个函数作一个介绍,它提供的接口与C标准库time.h基本一致.然后再介 ...

  8. Qt Assistant 工作机制

    Qt Assistant 是Qt 的助手,我们在看帮助的时候要用到的,它可以记住上一次你访问的位置. 1.   所以在你调用Qt Assistant为我们自己的程序写help的时候要记得修改qhcp配 ...

  9. Git 如何回到过去,然后 再 回到将来

    回到过去: git log 然后 git reset --hard commit ID (那段长长代码 40位) 再,回到将来git reflog 然后 git reset --hard 前面那个代码 ...

  10. OMX Codec详细解析

    概述 OMX Codec是stagefrightplayer中负责解码的模块. 由于遵循openmax接口规范,因此结构稍微有点负责,这里就依照awesomeplayer中的调用顺序来介绍. 主要分如 ...