zoukankan      html  css  js  c++  java
  • LeetCode

    Search for a Range

    2014.2.13 20:27

    Given a sorted array of integers, find the starting and ending position of a given target value.

    Your algorithm's runtime complexity must be in the order of O(log n).

    If the target is not found in the array, return [-1, -1].

    For example,
    Given [5, 7, 7, 8, 8, 10] and target value 8,
    return [3, 4].

    Solution1:

      Apparently this problem is a test for binary search. You could either use the function upper_bound() and lower_bound() provided by STL <algorithm>, or write one for yourself.

      Total time complexity is O(log(n)). Space complexity is O(1).

    Accepted code:

     1 // 1AC, the code can be shorter, but..1AC is just fine.
     2 class Solution {
     3 public:
     4     vector<int> searchRange(int A[], int n, int target) {
     5         vector<int> res;
     6         int ll, rr, mm;
     7         int i1, i2;
     8         
     9         if (A == nullptr || n <= 0) {
    10             res.push_back(-1);
    11             res.push_back(-1);
    12             return res;
    13         }
    14         
    15         if (n == 1) {
    16             if (A[0] == target) {
    17                 res.push_back(0);
    18                 res.push_back(0);
    19             } else {
    20                 res.push_back(-1);
    21                 res.push_back(-1);
    22             }
    23             return res;
    24         }
    25         
    26         if (target < A[0] || target > A[n - 1]) {
    27             res.push_back(-1);
    28             res.push_back(-1);
    29             return res;
    30         }
    31         
    32         if (target == A[0]) {
    33             i1 = 0;
    34         } else {
    35             ll = 0;
    36             rr = n - 1;
    37             while (rr - ll > 1) {
    38                 mm = (ll + rr) / 2;
    39                 if (A[mm] < target) {
    40                     ll = mm;
    41                 } else {
    42                     rr = mm;
    43                 }
    44             }
    45             if (A[rr] != target) {
    46                 res.push_back(-1);
    47                 res.push_back(-1);
    48                 return res;
    49             } else {
    50                 i1 = rr;
    51             }
    52         }
    53         
    54         if (target == A[n - 1]) {
    55             i2 = n - 1;
    56         } else {
    57             ll = 0;
    58             rr = n - 1;
    59             while (rr - ll > 1) {
    60                 mm = (ll + rr) / 2;
    61                 if (A[mm] > target) {
    62                     rr = mm;
    63                 } else {
    64                     ll = mm;
    65                 }
    66             }
    67             if (A[ll] != target) {
    68                 res.push_back(-1);
    69                 res.push_back(-1);
    70                 return res;
    71             } else {
    72                 i2 = ll;
    73             }
    74         }
    75         res.push_back(i1);
    76         res.push_back(i2);
    77         return res;
    78     }
    79 };

    Solution2:

      The STL version.

    Accepted code:

     1 // 1AC, very smooth.
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 class Solution {
     6 public:
     7     vector<int> searchRange(int A[], int n, int target) {
     8         vector<int> res;
     9         
    10         res.push_back(-1);
    11         res.push_back(-1);
    12         if (A == nullptr) {
    13             return res;
    14         }
    15         
    16         int *px, *py;
    17         int ix, iy;
    18         
    19         px = lower_bound(A, A + n, target);
    20         ix = px - A;
    21         if (ix >= n || A[ix] != target) {
    22             // not found
    23             return res;
    24         }
    25         
    26         py = upper_bound(A, A + n, target);
    27         --py;
    28         iy = py - A;
    29         res[0] = ix;
    30         res[1] = iy;
    31         
    32         return res;
    33     }
    34 };
  • 相关阅读:
    [LeetCode 049] Group Anagrams
    [LeetCode 033] Search in Rotated Sorted Array
    [LeetCode 024] Swap Nodes in Pairs
    [LeetCode 016] 3Sum Closest
    [LeetCode 015] 3Sum
    [LeetCode 013] Roman to Integer
    [LeetCode 008] String to Integer (atoi)
    [LeetCode 007] Reverse Integer
    第四课:文件操作【解密】
    第三课:文件操作【逐步浅入,深入加解法】
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3548659.html
Copyright © 2011-2022 走看看