题目:34. Find First and Last Position of Element in Sorted Array
Given an array of integers nums
sorted in ascending order, 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]
.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
题意:
给一个升序排列的数列nums,和一个整数target,找出在target在nums中第一次和最后一次出现的位置。要求时间复杂度为O(logn)
分析:
根据题目要求的时间复杂度 很明显可以得出,本题需要使用 二分查找来完成。如下:
Code1 & C++:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int len = nums.size();
int i=0, j=len-1;
vector<int> ans(2, -1);
while(i<=j){
int mid = (i+j)/2;
if(nums[mid]>target){
while(mid>i && nums[mid]==nums[mid-1])--mid; //过滤掉相同的元素,下同
j = mid-1;
}else if(nums[mid]<target){
while(mid<j && nums[mid]==nums[mid+1])++mid;
i=mid+1;
}else{
int tmp = mid;
while(mid>=0 && nums[mid]==target)--mid;
ans[0]=mid+1;
while(tmp<len && nums[tmp]==target)++tmp;
ans[1]=tmp-1;
break;
}
}
return ans;
}
};
Result:
Runtime: 12 ms, faster than 97.67% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 10.9 MB, less than 44.18% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
通过以上结果发现,本解法还不是最优解。
换种写法,偶然提交居然得到了100%。。。。
Code2 & C++
static const auto s = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int len = nums.size();
int i=0, j=len-1, mid;
bool flag = false;
while(i<=j){
mid = (i+j)/2;
if(nums[mid]>target)j=mid-1;
else if(nums[mid]<target)i=mid+1;
else{
flag = true;
break;
}
}
if(flag){
vector<int> ans(2,-1);
i=0;
j=len-1;
while(i<=j){
mid = (i+j)/2;
if(nums[mid]<target)i=mid+1;
else j=mid-1;
}
ans[0] = i;
i=0;j=len-1;
while(i<=j){
mid = (i+j)/2;
if(nums[mid]>target)j=mid-1;
else i = mid+1;
}
ans[1] = j;
return ans;
}
return {-1,-1};
}
};
Result:
Runtime: 8 ms, faster than 100.00% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 10.5 MB, less than 98.71% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
至于code2为什么比code1要快一些? 还望各路大佬指点。在此先谢为敬!
下面来看python大法如何实现:
Code & Python
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
if target in nums:
return [nums.index(target), nums.index(target)+nums.count(target)-1]
return [-1,-1]
Result:
Runtime: 40 ms, faster than 75.99% of Python3 online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 13.9 MB, less than 5.06% of Python3 online submissions for Find First and Last Position of Element in Sorted Array.
一目了然,对比C++真的是便捷到哭。。。
那么,python + binarySearch呢?
Code & Python + Binary
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
nlen ,i ,j = len(nums), 0, len(nums)-1
while i<=j:
mid = (i+j)//2;
if nums[mid] < target:
while mid<j and nums[mid]==nums[mid+1]:mid+=1
i=mid+1
elif nums[mid] > target:
while mid>i and nums[mid] == nums[mid-1]:mid-=1
j=mid-1
else:
tmp = mid
while mid>=0 and nums[mid]==target:mid-=1
while tmp<nlen and nums[tmp]==target:tmp+=1
return [mid+1,tmp-1]
return [-1,-1]
Result:
Runtime: 44 ms, faster than 42.08% of Python3 online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 13.9 MB, less than 5.06% of Python3 online submissions for Find First and Last Position of Element in Sorted Array.
对比可以发现,和上面的python大法相比,在效率相差无几。。