zoukankan      html  css  js  c++  java
  • 60. Search Insert Position 【easy】

    60. Search Insert Position 【easy】

    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.

    Example

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

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

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

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

    Challenge 

    O(log(n)) time

    解法一:

     1 class Solution {
     2     /** 
     3      * param A : an integer sorted array
     4      * param target :  an integer to be inserted
     5      * return : an integer
     6      */
     7 public:
     8     int searchInsert(vector<int> &A, int target) {
     9         if (A.size() == 0) {
    10             return 0;
    11         }
    12         
    13         int start = 0;
    14         int end = A.size() - 1;
    15         
    16         while (start + 1 < end) {
    17             int mid = start + (end - start) / 2;
    18             
    19             if (A[mid] == target) {
    20                 return mid;
    21             }
    22             else if (A[mid] < target) {
    23                 start = mid;
    24             }
    25             else if (A[mid] > target) {
    26                 end = mid;
    27             }
    28         }
    29         
    30         if (target <= A[start]) {
    31             return start;
    32         }
    33         else if (target <= A[end]) {
    34             return end;
    35         }
    36         else if (target > A[end]) {
    37             return end + 1;
    38         }
    39     }
    40 };

    最后返回的时候要格外注意,按照模板来说,最后返回的start或者end中是有可能等于我们要找的数的,如果都没有找到后面还要补刀一个-1;对应这个题不用补刀,因为是要找插入的位置,必定会有一个合理的插入位置。

    解法二:

     1 public class Solution {
     2     public int searchInsert(int[] A, int target) {
     3         if (A == null || A.length == 0) {
     4             return 0;
     5         }
     6         int start = 0;
     7         int end = A.length - 1;
     8         int mid;
     9         
    10         if (target < A[0]) {
    11             return 0;
    12         }
    13         // find the last number less than target
    14         while (start + 1 < end) {
    15             mid = start + (end - start) / 2;
    16             if (A[mid] == target) {
    17                 return mid;
    18             } else if (A[mid] < target) {
    19                 start = mid;
    20             } else {
    21                 end = mid;
    22             }
    23         }
    24         
    25         if (A[end] == target) {
    26             return end;
    27         }
    28         if (A[end] < target) {
    29             return end + 1;
    30         }
    31         if (A[start] == target) {
    32             return start;
    33         }
    34         return start + 1;
    35     }
    36 }

    解法三:

     1 class Solution {
     2     /** 
     3      * param A : an integer sorted array
     4      * param target :  an integer to be inserted
     5      * return : an integer
     6      */
     7 public:
     8     int searchInsert(vector<int> &A, int target) {
     9         // find first position >= target
    10         if (A.size() == 0) {
    11             return 0;
    12         }
    13         
    14         int start = 0, end = A.size() - 1;
    15         while (start + 1 < end) {
    16             int mid = (end - start) / 2 + start;
    17             if (A[mid] >= target) {
    18                 end = mid;
    19             } else {
    20                 start = mid;
    21             }
    22         }
    23         
    24         if (A[start] >= target) {
    25             return start;
    26         }
    27         if (A[end] >= target) {
    28             return end;
    29         }
    30         
    31         return A.size();
    32     }
    33 };

    大神解法,非常简便!

  • 相关阅读:
    windows下 CodeBlock13-12 实验 C++11 测试
    用矩阵运算实现最小二乘法曲线拟合算法
    winXP 系统下ubuntu-12.04 硬盘安装
    TCP服务器并发编程构架:完成端口IOCP模式
    TCP服务器并发编程构架:完成例程IRP模式
    续:双缓存队列_模板类
    双缓存静态循环队列(三)
    如何在只知道SQL_ID时,查询到此sql语句的执行计算机名称(是两三天前的SQL语句)
    RMAN Catalog 和 Nocatalog 的区别
    异机恢复后ORA-01152错误解决
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7543522.html
Copyright © 2011-2022 走看看