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

精选leecode300题,二分查找中等题436 #137

Open
tjl8787 opened this issue Nov 21, 2023 · 0 comments
Open

精选leecode300题,二分查找中等题436 #137

tjl8787 opened this issue Nov 21, 2023 · 0 comments

Comments

@tjl8787
Copy link

tjl8787 commented Nov 21, 2023

参考秀哥代码时,这个例子会出问题:输入intervals =[[4,4]] 输出[-1] 预期结果[0]
添加一个if判断看是否会出现只有一个子数组内两个元素相等情况,有就返回{0},成功通过
vector findRightInterval(vector<vector>& intervals) {
int len=intervals.size();
if(len<=1){
if(intervals[0][1]==intervals[0][0]){
return {0};
}
else{
return {-1};
}
}
map<int,int> m;
vector res;
res.reserve(len);
for(int i=0;i<len;i++){
m[intervals[i][0]]=i;
}
for(auto& a:intervals){
auto it=m.lower_bound(a[1]);
if(it!=m.end()){
res.push_back(it->second);
}
else{
res.push_back(-1);
}
}
return res;
}

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

1 participant