zoukankan      html  css  js  c++  java
  • LeetCode: Search Insert Position 解题报告

    Search Insert Position

    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:

    用九章算法的模板查找结束后判断一下即可。:

     1 public int searchInsert1(int[] A, int target) {
     2         if (A == null || A.length == 0) {
     3             return 0;
     4         }
     5         
     6         int left = 0;
     7         int right = A.length - 1;
     8         
     9         while (left < right - 1) {
    10             int mid = left + (right - left) / 2;
    11             int num = A[mid];
    12             
    13             if (num == target) {
    14                 return mid;
    15             } else if (num < target) {
    16                 left = mid + 1;
    17             } else {
    18                 right = mid - 1;
    19             }
    20         }
    21         
    22         // bug 1: should use <=
    23         if (target <= A[left]) {
    24             return left;
    25         // bug 2: should use <= . consider that may the result exit in left or right.    
    26         } else if (target <= A[right]) {
    27             return right;
    28         }
    29         
    30         return right + 1;
    31     }
    View Code

    SOLUTION 2:

    也可以很简洁:

    http://fisherlei.blogspot.com/2013/01/leetcode-search-insert-position.html

    http://blog.csdn.net/fightforyourdream/article/details/14216321

    这样可以word的原因是:

    1. 当target存在,当然返回mid.

    2. 当target大于所有的数。则l, r会跑到最右边,并且l会继续跑出去一格,也就是l会指向 len,也就是要找的值。

    3. 当target小于所有的数。l,r跑到最左边,并且r会继续往左移动一格,l指向目标位置。

    4. 当target小于某数a,并大于某数b。那么l, r中止时,r会在b,l 会在a,l 指向目标位置。

    若是找不到target, 循环结束后l 的值是 与target最接近但是 > target 的数在数组中的位置。

     1 // sol 2:
     2     public int searchInsert(int[] A, int target) {
     3         if (A == null || A.length == 0) {
     4             return 0;
     5         }
     6         
     7         int left = 0;
     8         int right = A.length - 1;
     9         
    10         while (left <= right) {
    11             int mid = left + (right - left) / 2;
    12             int num = A[mid];
    13             
    14             if (num == target) {
    15                 return mid;
    16             } else if (num < target) {
    17                 left = mid + 1;
    18             } else {
    19                 right = mid - 1;
    20             }
    21         }
    22         
    23         return left;
    24     }
    View Code

    GTIHUB:

    https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/SearchInsert.java

  • 相关阅读:
    UDP and netstat
    UDP learn by Python3
    UDP headers and checksum
    routetrace
    IPv4 headers
    Commands for IP
    IP checksum
    POJ 3667 Hotel 线段树处理区间信息
    【枚举】Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) Div2C题
    二分图最大匹配模板 HDU1083
  • 原文地址:https://www.cnblogs.com/yuzhangcmu/p/4196885.html
Copyright © 2011-2022 走看看