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

  1. public class Solution {
  2. public int searchInsert(int[] nums, int target) {
  3. if(nums==null) return 0;
  4. if(target <= nums[0]) return 0;
  5. for(int i=0; i<nums.length-1; i++){
  6. if(target > nums[i] && target <= nums[i+1]){
  7. return i+1;
  8. }
  9. }
  10. return nums.length;
  11. }
  12. }

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))

  1. public class Solution {
  2. public int searchInsert(int[] nums, int target) {
  3. if (nums == null || nums.length == 0)
  4. return 0;
  5. int start = 0, end = nums.length - 1, mid = (end - start) / 2 + start;
  6. while (start <= end) {
  7. mid = (end - start) / 2 + start;
  8. if (nums[mid] == target)
  9. return mid;
  10. else if (nums[mid] < target)
  11. start = mid + 1;
  12. else
  13. end = mid - 1;
  14. }
  15. return start;
  16. }
  17. }

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. 方案:解决 wordpress 中 gravatar 头像被墙问题

    Gravatar头像具有很好的通用性,但是却遭到了无辜的拦截,对于无法加载头像URL,我们在WordPress系统中通过修改默认的URL链接可以达到恢复头像的功能. 修改文件路径为 /wp-inclu ...

  2. Merge Sorted Array 解答

    Question Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  3. 再造轮子之网易彩票-第二季(IOS 篇 by sixleaves)

    02-彩票项目第二季 2.封装SWPTabBar方式一 接着我们思考如何进行封装.前面已经将过了为什么要封装, 和封装达到的效果.这里我们主要有两种封装方式,分别是站在不同的角度上看待问题.虽然角度不 ...

  4. postgresql C/C++ API 接口

    1,postgresql学习uri推荐 http://www.php100.com/manual/PostgreSQL8/ http://www.php100.com/manual/PostgreSQ ...

  5. SimHash算法

    短文本合并重复(去重)的简单有效做法 - 旁观者 - 博客园 短文本合并重复(去重)的简单有效做法 SimHash算法 - ACdreamer - 博客频道 - CSDN.NET SimHash算法

  6. jdbc资料收集

    1.Hibernate史上最简单的Hibernate入门简介http://blog.csdn.net/doodoofish/article/details/43207/ jdbc不足 尽管JDBC在J ...

  7. linux时钟管理

    ref https://access.redhat.com/solutions/18627 在el5中 如何查看系统现在使用的clock source是什么? 答: 方式1:需要说明的是不能保证这个两 ...

  8. 分割视图控制器(UISplitViewController) 改_masterColumnWidth 导致在 IOS 10中出现闪退

    默认UISplitViewController的Master和Detail的宽度是固定的,可以通过下面的方式来改变 [splitViewController setValue:[NSNumber nu ...

  9. Java动态 遍历List 时删除List特征元素 异常问题 及解决方案总结

    首先.这是一个极其简单的问题,大牛可忽略.新手可能会遇到,Java中遍历某个List 时删除该List元素 会抛出异常. 这一个简单的问题再高手严重不值一提,但新手可能会比較困惑,用哪种方式能够安全有 ...

  10. mbed OS - ARM关于物联网(IoT)的战略布局

    关于IoT 在刚刚过去的ARMTECHCON2014(Santa Clara Convention Center)第1天会议,首要的keynote就是ARM针对建立物联网(InternetOf Thi ...