34. Find First and Last Position of Element in Sorted Array

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

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

Example

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

 1 class Solution:
 2     def searchRange(self, nums: List[int], target: int) -> List[int]:
 3         def search(x):
 4             lo, hi = 0, len(nums) - 1           
 5             while lo <= hi:
 6                 mid = (lo + hi) // 2
 7                 if nums[mid] < x:
 8                     lo = mid + 1
 9                 else:
10                     hi = mid - 1                    
11             return lo
12         
13         lo = search(target)
14         hi = search(target + 1) - 1
15         
16         if lo < 0 or lo >= len(nums) or hi < 0 or hi >= len(nums) or nums[lo] != target:
17             return [-1, -1]
18 
19         if lo <= hi:
20             return [lo, hi]
21                 
22         return [-1, -1]
23         

 

分析:

用binary search分别找出最左和最右index就可以了。

 1 public class Solution {
 2     public int[] searchRange(int[] A, int target) {
 3         if (A == null || A.length <= 0) return new int[] {-1, -1};
 4         int leftIndex = getLeftIndex(A, target);
 5         int rightIndex = getRightIndex(A, target);
 6         
 7         if (leftIndex < 0 || leftIndex >= A.length || A[leftIndex] != target) {
 8             leftIndex = -1;
 9         }
10         
11         if (rightIndex < 0 || rightIndex >= A.length || A[rightIndex] != target) {
12             rightIndex = -1;
13         }
14     
15         return new int[]{leftIndex, rightIndex};
16     }
17 
18     public int getLeftIndex(int[] A, int target) {
19         int start = 0, end = A.length - 1;
20         while (start <= end) {
21             int mid = (start + end) / 2;
22             if (A[mid] >= target) {
23                 end = mid - 1;
24             } else {
25                 start = mid + 1;
26             }
27         }
28         return start;
29     }
30 
31     public int getRightIndex(int[] A, int target) {
32         int start = 0, end = A.length - 1;
33         while (start <= end) {
34             int mid = (start + end) / 2;
35             if (A[mid] > target) {
36                 end = mid - 1;
37             } else {
38                 start = mid + 1;
39             }
40         }
41         return end;
42     }
43 }

 

 1 public class Solution {
 2     public int[] searchRange(int[] A, int target) {
 3         if (A == null || A.length <= 0) return new int[] {-1, -1};
 4         return new int[]{getLeftIndex(A, target), getRightIndex(A, target)};
 5     }
 6 
 7     public int getLeftIndex(int[] A, int target) {
 8         int start = 0, end = A.length - 1;
 9         while (start <= end) {
10             int mid = (start + end) / 2;
11             if (A[mid] == target) {
12                 if (mid == 0 || A[mid] != A[mid - 1]) {
13                     return mid;
14                 } else {
15                     end = mid - 1;
16                 }
17             } else if (A[mid] > target) {
18                 end = mid - 1;
19             } else {
20                 start = mid + 1;
21             }
22         }
23         return -1;
24     }
25 
26     public int getRightIndex(int[] A, int target) {
27         int start = 0, end = A.length - 1;
28         while (start <= end) {
29             int mid = (start + end) / 2;
30             if (A[mid] == target) {
31                 if (mid == A.length - 1 || A[mid] != A[mid + 1]) {
32                     return mid;
33                 } else {
34                     start = mid + 1;
35                 }
36             } else if (A[mid] > target) {
37                 end = mid - 1;
38             } else {
39                 start = mid + 1;
40             }
41         }
42         return -1;
43     }
44 }

 

posted @ 2016-07-05 08:56  北叶青藤  阅读(138)  评论(0)    收藏  举报