段错误,但IDE运行没问题
https://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?tpId=13&tqId=11176&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking -- 题目链接
提示说段错误,但是本地IDE上运行没有错误,大神帮忙看下。算法思想:挨个入栈,遇到比栈顶元素大的就将栈清空,出栈序列要是有序的则判定为true 否则false
代码:
class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence)
{
if(sequence.size()==0)
return true ;
vector<int> res ;
vector <int> stack ;
stack.push_back(sequence[0]) ;
for(int i=1 ;i<sequence.size ();++i)
{
if( stack.back() <= sequence[i] )
{
while(stack.size()>0)
{
res.push_back( stack.back()) ;
stack.pop_back () ;
}
}
stack.push_back(sequence[i]) ;
}
for(int i=0 ;i<res.size()-1 ;++i)
if(res[i+1]<res[i])
return false ;
return true ;
}
};