LeetCode solution Report-Search for a Range
Question:
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].
Click solution: Search for a Range
Analysis: it is required that the target array be located in the range of the sorted series.
The question is not difficult. Pay attention to the time complexity O (logn), which is the key to the question. There are three solutions:
1)Direct brute force traversal (timeout ):Directly use the for loop to traverse from left to right. The time complexity is O (n), and timeout. First find the record for the first time nums [I] = target, array [0] = I, and then continue traversing until nums [I]! = Target, array [1] = I.
2)Binary Search (feasible ):First find nums [I] = target, no matter whether I is the left or right boundary, or the middle value, that is, find the first nums [I] = target, and then on the basis of the found I, traverse to the right to find the right boundary, traverse to the left, and find the left boundary. Note that when the first I = 0 (or I = nums. length-1), then the left (or right) boundary is found. You only need to find the right (or left) Boundary and add a judgment.
3)Double pointer hair (feasible ):The double pointers left and right point to the start point of the array, that is, left = 0, right = nums. length-1, and then traverse from left to right, find the first nums [I] = target, left = I, then left is the left boundary, and then traverse from right to left, find the first nums [I] = target, right = I, and right is the right border.
As shown below:
Java code: Accepted
Binary Search:
public class Solution { public int[] searchRange(int[] nums, int target) { int left = 0; int right = nums.length - 1; //term, termq for termporary variable, save the first middle int tem = 0; int tem1 = 0; //sign whether find target int flag = 0; int[] array = new int[] { -1, -1 }; //Binary search to find first middle nums[middle] = target while (left <= right) { int middle = (left + right) / 2; if (nums[middle] == target) { tem = middle; tem1 = middle; flag = 1; break; } else if (nums[middle] < target) { left = middle + 1; } else { right = middle - 1; } } //if not return[-1,-1] if (flag == 0) { return array; } //find the right boundary if(tem != nums.length - 1){ while (tem < nums.length) { if ((tem + 1 < nums.length) && nums[tem] == nums[tem + 1]) { tem++; } else { array[1] = tem; break; } } }else{ array[1] = tem; } //find left boundary if(tem1 != 0){ while (tem1 > -1) { if ((tem1 - 1 > -1) && nums[tem1] == nums[tem1 - 1]) { tem1--; } else { array[0] = tem1; break; } } }else{ array[0] = tem1; } return array; }}
Tow Points Search:
public class Solution { public int[] searchRange(int[] nums, int target) { int[] array = new int[]{-1,-1}; //tow points for search nums[left] == target and nums[right] == target int left = 0; int right = nums.length - 1; //flagL for left index is found and flagR for right index is found int flagL = 0; int flagR = 0; //Find left index while(left <= right){ if(flagL == 0 && nums[left] == target){ array[0] = left; flagL = 1; break; }else{ left ++; } } //Find right index while(right >= left){ if(flagR == 0 && nums[right] == target){ array[1] = right; flagR = 1; break; }else{ right --; } } //No find target if(flagL == 0 && flagR == 0){ return array; } return array; }}
Python code Accepted:
Two Points Search:
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ #array for the result array = [-1,-1] #flagL for left index is found flagL = 0 #flagR for right index is found flagR = 0 #two points left and righr left = 0 right = len(nums) - 1 #found left index while(left <= right): if(nums[left] == target): array[0] = left flagL = 1 break else: left += 1 #found right index while(right >= left): if(nums[right] == target): array[1] = right flagR = 1 break else: right -= 1 #no found target if(flagL == 0 and flagR == 0): return array return array