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 };

    大神解法,非常简便!

  • 相关阅读:
    数论练习
    AC自动机*
    矩阵乘法*
    概率期望*
    组合数学练习*
    图论升级*
    【终端使用】"su"命令切换用户
    【终端使用】"which"命令可以查看执行命令所在的位置
    【终端使用】"usermod"命令 和 组(包括:主组、附加组)
    Ubuntu 18.04安装 MySQL 8.0+版本的数据库
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7543522.html
Copyright © 2011-2022 走看看