74-Search for a Range

本文介绍了一种在有序数组中寻找特定目标值起始与结束位置的算法,利用二分查找提高效率,确保时间复杂度为O(logn+k),其中k为目标元素的重复次数。
  1. Search for a Range My Submissions QuestionEditorial Solution
    Total Accepted: 83626 Total Submissions: 285587 Difficulty: Medium
    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].

题意:在有序数组中找到目标元素并返回其对应的索引区间
思路:用二分查找找到中间元素位置,并向两边扩展

时间复杂度:O(logn+k)k
空间复杂度:O(1)

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int n = nums.size();
        int beg = 0,end = n-1;
        int mid;
        while(beg<=end){  //二分查找目标元素 
            mid = (beg+end)/2;
            if(nums[mid]>target)end = mid-1;
            else if(nums[mid]<target)beg = mid+1;
            else break;
        }
        vector<int> res(2,-1);
        cout<<"mid:"<<mid<<" "<<nums[mid]<<endl;
        if(nums[mid]==target){    //找到了 
            beg =0,end = n-1;
            int fir=mid,sec=mid;
            while(sec<=end&&nums[sec]==target)++sec;  //向右边扩展 
            while(fir>=0&&nums[fir]==target)--fir;    //向左边扩展 
            res[0]=fir+1;res[1]=sec-1;                //确定最终位置 
        }
        return res;                  
    }
};
### Lexicographic Search Algorithm Implementation and Explanation Lexicographic order is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters. This ordering can be applied to any set of items that have an inherent total order. In computer science, lexicographical algorithms often involve searching or sorting strings in dictionary (lexicographical) order. The following Python code demonstrates how one might implement a simple version of a lexicographic search function over two lists containing comparable elements such as integers or characters: ```python def lexi_search(list1, list2): min_length = min(len(list1), len(list2)) for i in range(min_length): if list1[i] < list2[i]: return -1 # List1 comes before List2 lexically elif list1[i] > list2[i]: return 1 # List2 comes before List1 lexically # If all compared elements were equal but lengths differ if len(list1) != len(list2): return -1 if len(list1) < len(list2) else 1 return 0 # Both sequences are identical up until end point reached ``` This implementation compares corresponding positions between `list1` and `list2`. When encountering unequal values at some position \(i\), it returns immediately indicating which sequence precedes the other according to standard comparison rules. In case both sequences share common prefixes yet vary only by length, shorter ones will come first when sorted lexicographically[^1]. For more complex scenarios involving string manipulations or custom objects with defined comparators, additional logic would need to be incorporated into this basic framework depending upon specific requirements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值