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

一、题目说明

题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n)。题目的难度是Medium!

二、我的解答

这个题目还是二分查找(折半查找),稍微变化一下。target==nums[mid]后,需要找前面、后面的值是否=target。

一次写出来,bug free,熟能生巧!怎一个爽字了得!

#include<iostream>
#include<vector>
using namespace std;
class Solution{
    public:
        vector<int> searchRange(vector<int>& nums, int target){
            vector<int> res;
            if(nums.size()<1){
                res.push_back(-1);
                res.push_back(-1);
                return res;
            }

            int begin = 0;
            int end = nums.size()-1;
            int mid = -1;
            while(begin <= end){
                mid = (begin + end) / 2;
                if(nums[mid] == target){
                    begin = mid;
                    while(begin>0 && nums[begin] == target){
                        begin--;
                    }
                    if(nums[begin]==target){
                        res.push_back(begin);
                    }else{
                        res.push_back(begin+1);
                    }

                    end = mid;
                    while(end<nums.size()-1 && nums[end] == target){
                        end++;
                    }
                    if(nums[end]==target){
                        res.push_back(end);
                    }else{
                        res.push_back(end-1);
                    }
                    return res;
                }else if(nums[mid] < target){
                    begin = mid + 1;
                }else{
                    end = mid - 1;
                }
            }
            //未找到
            res.push_back(-1);
            res.push_back(-1);
            return res;
        }
};
int main(){
    Solution s;
    vector<int> nums = {5,7,7,8,8,10};
    vector<int> r = s.searchRange(nums,8);
    for(vector<int>::iterator it=r.begin();it!=r.end();it++){
        cout<<*it<<" ";
    }

    r = s.searchRange(nums,6);
    for(int i=0;i<r.size();i++){
        cout<<r[i]<<" ";
    }
    return 0;
} 

代码性能:

Runtime: 12 ms, faster than 38.75% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 10.4 MB, less than 70.33% of C++ online submissions for Find First and Last Position of Element in Sorted Array.

三、改进

上一个题目,发现mid = begin + (end - begin) / 2;,性能比mid = (begin + end) / 2高很多。

性能提高到:

Runtime: 8 ms, faster than 86.11% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 10.4 MB, less than 82.42% of C++ online submissions for Find First and Last Position of Element in Sorted Array.

这究竟为何,哪位大神指导,请指点。不胜感激!!!

此处不要提mid = (begin + end) / 2可能溢出。。。

原文地址:https://www.cnblogs.com/siweihz/p/12238627.html

时间: 2024-08-30 02:33:48

刷题34. Find First and Last Position of Element in Sorted Array的相关文章

Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 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 v

[leetcode][34] Find First and Last Position of Element in Sorted Array

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)

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

19.2.2 [LeetCode 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].

[二分搜索] leetcode 34 Find First and Last Position of Element in Sorted Array

problem:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 经典二分搜索题.要点是改变low或high的时候把当前数字mid也包含进来,因为它也可能是结果. class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 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

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].

【LeetCode】【找元素】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, -

Find First and Last Position of Element in Sorted Array

问题:给定一个有序数组和一个目标值,输出目标值在数组中的起始位置和终止位置,如果目标值不在数组中,则输出[-1,-1] 示例: 输入:nums = [1,2,3,5,5,7] target = 5 输出:[3,4] 输入:nums = [1,5,8,9] target = 7 输出:[-1,-1] 解决思路:二分查找直到找到第一个目标值,再对目标值左右进行二分查找 Python代码: class Solution(object): def searchRange(self, nums, targ