#34排序数组查首尾位置
medium,我写的:1 暴力
vector<int> searchRange(vector<int>& nums, int target) {
int start=-1;
int end=-1;
for(int i=0;i<nums.size();i++){
if(nums[i]==target && start==-1) start=i;
if(nums[i]==target && start>-1) end=i;
}
return {start,end};
}
我写的,做了个类似二分搜索的方法:
vector<int> searchRange(vector<int>& nums, int target) {
int start=0;
int end=nums.size()-1;
int res=-1;
while(start<=end){
//cout<<"start: "<<start<<", end: "<<end<<endl;
int mid=(start+end)/2;
if(nums[mid]==target){
res=mid;
break;
}
if(target<nums[mid]) end=mid-1;
if(target>nums[mid]) start=mid+1;
}
//cout<<res<<endl;
start=res; end=res;
while(start>=0 && nums[start]==target){
start--;
}
while(end<nums.size() && nums[end]==target){
end++;
}
//if(nums.size()==1) return {0,0};
if(res==-1) return{-1,-1};
return {start+1,end-1};
}
随想录:从两头都做类似二分搜索
vector<int> searchRange(vector<int>& nums, int target) {
int leftBorder = getLeftBorder(nums, target);
int rightBorder = getRightBorder(nums, target);
// 情况一
if (leftBorder == -2 || rightBorder == -2) return {-1, -1};
// 情况三
if (rightBorder - leftBorder > 1) return {leftBorder + 1, rightBorder - 1};
// 情况二
return {-1, -1};
}
int getRightBorder(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int rightBorder = -2; // 记录一下rightBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] > target) {
right = middle - 1;
} else { // 寻找右边界,nums[middle] == target的时候更新left
left = middle + 1;
rightBorder = left;
}
}
return rightBorder;
}
int getLeftBorder(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int leftBorder = -2; // 记录一下leftBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] >= target) { // 寻找左边界
right = middle - 1;
leftBorder = right;
} else {
left = middle + 1;
}
}
return leftBorder;
}
#922 按奇偶排序数组II
我的解法,有点蠢:
vector<int> sortArrayByParityII(vector<int>& nums) {
int cnt=0;
vector<int> odd;
vector<int> even;
for(int i=0;i<nums.size();i++){
if(i%2!=0 && nums[i]%2==0) odd.push_back(i);
if (i%2==0 && nums[i]%2!=0) even.push_back(i);
}
for(int i=0;i<odd.size();i++){
swap(nums[odd[i]],nums[even[i]]);
}
return nums;
}
inplace解法: 把odd idx放的偶数,给换到even idx放的奇数
注意j是从1开始,而且每轮i,j都是继续增加不回去
空间表现很好,但时间表现很一般
vector<int> sortArrayByParityII(vector<int>& nums) {
int j=1;
for(int i=0;i<nums.size();i+=2){
if(nums[i]%2){
while(j<nums.size() && nums[j]%2) j+=2;
swap(nums[i],nums[j]);
}
}
return nums;
}
#35搜索插入位置
要求复杂度O log n ,我写的:
int searchInsert(vector<int>& nums, int target) {
int start=0;
int end=nums.size()-1;
while(start<=end){
int mid=(start+end)/2;
//cout<<"mid: "<<mid<<endl;
if(nums[mid]==target) return mid;
else if(mid==start) {
if(start+1<nums.size() && target>nums[start+1]) return start+2;
return target>nums[start]?start+1:start;
}
if(nums[mid]>target) end=mid-1;
if(nums[mid]<target) start=mid+1;
}
return -1;
}
随想录写的:所有情况都能统一变成return end+1或者return start
int searchInsert(vector<int>& nums, int target) {
int start=0;
int end=nums.size()-1;
int mid;
while(start<=end){
mid=(start+end)/2;
if(nums[mid]==target) return mid;
if(nums[mid]>target) end=mid-1;
if(nums[mid]<target) start=mid+1;
}
return end+1;
}
这里没想明白为什么不对,有空想想