2024每日刷题(133)
Leetcode—946. 验证栈序列
实现代码
class Solution {
public:bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {int left = 0;for(int i = 0; i < popped.size(); i++) {while(left < pushed.size() && pushed[left] != popped[i]) {if(st.empty()) {// 栈为空st.push(pushed[left++]);} else {// 栈不为空if(st.top() == popped[i]) {st.pop();break;}st.push(pushed[left++]);}}if(left < pushed.size() && pushed[left] == popped[i]) {left++;} else if(!st.empty() && left >= pushed.size()) {if(st.top() == popped[i]) {st.pop();} else {return false;}}}if(st.empty()) {return true;} else {return false;}}
private:stack<int> st;
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!