Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of push-in and pop-up sequence of C++ stack

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how C++ to solve the stack push-and-pop sequence problem, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Next, let's go to know it!

# define _ CRT_SECURE_NO_WARNINGS 1#include#includeusing namespace std;#include#include/* question requires: enter two integer sequences. The first sequence represents the push-in order of the stack. Please determine whether the second sequence is the pop-up order of the stack. Assume that all the numbers pushed into the stack are not equal. For example, sequence 1, 2, 3, 4, 5 is the push-in order of a stack, and sequence 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 4, 5, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 4, 5, 4, 4, 5, 4, 5, 4, 4, 5, 4, 5, 4, 5, 4, 5, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 (note: the length of the two sequences is equal) * / / * solution: use a stack to insert the elements in pushV in turn. If the top element of the stack is equal to the pos element in popV, then pop the top element of the stack and let pos point to the end of element insertion in the next element pushV. If the stack is empty, the condition is met, otherwise it is not satisfied. * / class Solution {public: bool IsPopOrder (vector pushV, vector popV) {stack s; if (pushV.size ()! = popV.size ()) {return false;} int I = 0; int j = 0; while (I < pushV.size ()) {s.push (PushV [I + +]) While (! s.empty () & & s.top () = = popV [j]) {s.pop (); jobless;}} if (s.empty ()) {return true;} else {return false }; int main () {vectorv1 {1, 2, 3, 4 vectorv2 5}; Solution s; int a=s.IsPopOrder (v1, v2); cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report