• 14. First Position of Target 【easy】


    14. First Position of Target 【easy】

    For a given sorted array (ascending order) and a targetnumber, find the first index of this number in O(log n) time complexity.

    If the target number does not exist in the array, return -1.

    Example

    If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.

    Challenge 

    If the count of numbers is bigger than 2^32, can your code work properly?

    解法一:

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

    解法二:

     1 class Solution {
     2 public:
     3 
     4     int find(vector<int> &array, int start, int end, int target) {
     5         if (start > end) {
     6             return -1;
     7         }
     8         
     9         int mid = start + (end - start) / 2;
    10         
    11         if (array[mid] == target) {
    12 
    13             if (array[mid - 1] != target) {
    14                 return mid;
    15             }
    16 
    17             return find(array, start, mid - 1, target);
    18         }
    19         else if (array[mid] > target) {
    20             return find(array, start, mid - 1, target);
    21         }
    22         else if (array[mid] < target) {
    23             return find(array, mid + 1, end, target);
    24         }
    25         
    26     }
    27 
    28     /**
    29      * @param nums: The integer array.
    30      * @param target: Target number to find.
    31      * @return: The first position of target. Position starts from 0. 
    32      */
    33     int binarySearch(vector<int> &array, int target) {
    34         // write your code here
    35         
    36         int start = 0;
    37         int end = array.size();
    38         
    39         return find(array, start, end, target);
    40         
    41     }
    42 };
  • 相关阅读:
    if——while表达式详解
    java算法:抽象数据类型ADT
    java算法:FIFO队列
    Android_NetworkInfo以及判断手机是否联网
    java算法:堆栈ADT及实例
    java算法:数据项
    java算法:一流的ADT
    java算法:复合数据结构
    java算法:字符串
    java算法:基于应用ADT例子
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7543862.html
Copyright © 2020-2023  润新知