当前位置:网站首页>Pop up and push in sequence of stack < difficulty coefficient >
Pop up and push in sequence of stack < difficulty coefficient >
2022-06-28 10:16:00 【Hua Weiyun】
Title Description : Enter two sequences of integers , The first sequence represents the push order of the stack , Please determine if the second sequence is likely to be the pop-up sequence of the stack . Suppose that all the Numbers pushed are not equal . For example, the sequence 1,2,3,4,5 Is the push order of a stack , Sequence 4,5,3,2,1 Is a popup sequence corresponding to the stack sequence , but 4,5,3,2,1 It cannot be a popup sequence of the push sequence .
Tips :
- 0 <= pushV.length == popV.length <= 1000
- -1000 <= pushV[i] <= 1000
- pushV All numbers are different
Example 1:
Input :
[1,2,3,4,5],[4,5,3,2,1]
Return value :
true
explain :
Can pass
push(1) => push(2) => push(3) => push(4) => pop() => push(5) => pop() => pop() => pop() => pop()
In this order, we get [4,5,3,2,1] This sequence , return true.
Example 2:
Input :
[1,2,3,4,5],[4,3,5,1,2]
Return value :false
explain :
Because it is [1,2,3,4,5] Press in sequence of ,[4,3,5,1,2] Pop up order , requirement 4,3,5 Must be in 1,2 Front press in , And 1,2 Can't pop up , But in this order ,1 Can't in 2 Pop up before , So it can't form , return false.
🧷 platform :Visual studio 2017 && windows
The core idea : We have come across multiple-choice questions before this question . The essence of this problem is to simulate the characteristics of the stack “Last In First Out”.
Here, a stack is defined to simulate , It doesn't matter ,pushi First in stack , And then ++, The order of getting out of the stack must be after entering the stack , So you need to judge every time you enter the stack pushi and popi Whether it is equal or not , Equal and equal ( And cycle out ), Otherwise, enter , Both of them can go to the end , It means it's a match .

nowcoder The original title is
class Solution {public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { stack<int> st; size_t pushi = 0, popi = 0; // When the pressing sequence ends, the result must be while(pushi < pushV.size()) { // Enter a data first , then ++ st.push(pushV[pushi++]); // Loop out the stack while(!st.empty() && st.top() == popV[popi]) { ++popi; st.pop(); } } //st It's empty , Description match return st.empty(); // ditto //return popi == popV.size(); }}; 边栏推荐
- 通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
- Naming rules and specifications for identifiers
- ffmpeg录音录像
- 桥接模式(Bridge)
- 无线模块透明传输技术的物联网应用案例
- 再见!IE浏览器,这条路由Edge替IE继续走下去
- [Unity]EBUSY: resource busy or locked
- ECS MySQL query is slow
- 爬虫小操作
- Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
猜你喜欢

接口自动化框架脚手架-利用反射机制实现接口统一发起端

MySQL cannot be opened. Flash back

Teach you how to handle the reverse SVG mapping of JS

如图 用sql行转列 图一原表,图二希望转换后

Redis sentinel cluster main database failure data recovery ideas # yyds dry goods inventory #

Unity AssetBundle asset packaging and asset loading
![[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)](/img/04/454bede0944f56ba69cddf6b237392.jpg)
[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)

How to distinguish and define DQL, DML, DDL and DCL in SQL

Matplotlib attribute and annotation

Realize an air conditioner with compose to bring you cool in summer
随机推荐
Matplotlib attribute and annotation
PHP curl forged IP address and header information code instance - Alibaba cloud
如何使用 DataAnt 监控 Apache APISIX
Realize an air conditioner with compose to bring you cool in summer
June training (day 28) - Dynamic Planning
再見!IE瀏覽器,這條路由Edge替IE繼續走下去
Instant messaging and BS architecture simulation of TCP practical cases
[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting
桥接模式(Bridge)
Ribbon core source code analysis
Au revoir! Navigateur ie, cette route Edge continue pour IE
appliedzkp zkevm(10)中的Transactions Proof
Generate token
Adapter mode
Unity 从服务器加载AssetBundle资源写入本地内存,并将下载保存的AB资源从本地内存加载至场景
OpenHarmony应用开发之二维码生成器
bad zipfile offset (local header sig)
bye! IE browser, this route edge continues to go on for IE
Composite pattern
Read PDF Text and write excel operation