![](/img/trans.png)
[英]Lower_bound not working on the last element of a vector with 3 elements
[英]lower_bound() return the last element
當我使用lower_bound()
函數時,我無法理解我想找到最后一個元素與沒有找到然后返回最后一個元素之間的區別。
這是我的代碼:
class Solution {
public:
bool isSubsequence(string s, string t) {
vector<vector<int>> temp(26);
for (int i = 0; i < t.length(); ++i)
temp[t[i]-'a'].push_back(i);
int index = -1;
for (int i = 0; i < s.length(); ++i) {
auto it = upper_bound(temp[s[i]-'a'].begin(), temp[s[i]-'a'].end(), index);
//if the last element is we want to find what will happen?
if (it == temp[s[i]-'a'].end()) return false;
index = *it;
}
return true;
}
};
如果最后一個元素是我們想找到會發生什么?
end()
不指向最后一個元素,它指向超越了最后一個元素。 重點:
將迭代器返回給定容器c或數組數組的末尾(即最后一個元素之后的元素)。
如果最后一個元素是您要查找的元素,則lower_bound
將返回end() - 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.