Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.3 查找区间解法不稳定 34. Find First and Last Position of Element in Sorted Array (Medium) #61

Open
JiaqiongLi opened this issue Feb 13, 2022 · 1 comment

Comments

@JiaqiongLi
Copy link

class Solution {
public:
vector searchRange(vector& nums, int target) {

    if(nums.empty()) return vector<int>{-1,-1};
    int lower = lower_bound(nums, target);
    int upper = upper_bound(nums, target)-1;
    
    //if(nums[lower] != target || lower==nums.size()) return vector<int>{-1,-1}; 
    // there is a trap in this place if we use left open right closed interval
    // As lower pointer is possible to be out of the nums vector, nums[lower] may return errors
    
    if (lower == nums.size() || nums[lower] != target) return vector<int>{-1, -1};
    return vector<int>{lower, upper};
        
}
@changgyhub
Copy link
Owner

Sorry I don't understand your question - since in the code we are checking lower == nums.size() before nums[lower] != target, it should be impossible to query out of bound indices?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants