当前位置:网站首页>31. stack push in and pop-up sequence
31. stack push in and pop-up sequence
2022-06-12 05:18:00 【Be your goat】
The finger of the sword Offer 31. Pressure into the stack 、 Pop-up sequence
Ideas : Stack simulation
- initialization : Auxiliary stack stack, Pop up the index of the sequence i
- Traverse the stack sequence , Each element is marked as num:
- Elements num Push ;
- Loop out the stack : if stack Top element of = Pop up sequence elements popped[i], Perform stack out and i++
- Return value : if stack It's empty , Then this pop-up sequence is legal
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> stk;
int i=0;
for(int n:pushed){
stk.push(n);
while(stk.size()&&stk.top()==popped[i]){
stk.pop();
++i;
}
}
return stk.empty();
}
};
Time complexity O(n)
Spatial complexity O(n)
边栏推荐
- 2022 "college entrance examination memory" has been packaged, please check!
- Detailed usage of vim editor
- ShanMeng and Beijing Adoption Day start NFT digital collection public offering
- WiFi smartconfig implementation
- Longest palindrome string
- Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
- MySQL5.7.21 Build For ARM
- Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
- Data processing and data set preparation
- IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
猜你喜欢

Computer network connected but unable to access the Internet

Summary of common interview questions in redis

20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song

Some problems of Qinglong panel

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)

Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class

Introduction to audio alsa architecture

Thingsboard create RCP widget

2022 "college entrance examination memory" has been packaged, please check!

Quickly get PCA (principal component analysis) (principle code case)
随机推荐
The server time zone value ‘Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than one time zone. You
2022“高考记忆” 已打包完成,请查收!
Force/release learning and sorting in IC Verification (6) research on the influence of wire type signals
Understanding of day16 array create query static and dynamic array array array performance in memory
Data processing and data set preparation
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Walking "daily question" and "DP"
Introduction to audio alsa architecture
MySQL5.7.21 Build For ARM
Parallélisation de l'entraînement accéléré TF. Données. Générateur de données
Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind
It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
Interview must ask: summary of ten classic sorting algorithms
Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021
Drive safety coding & troubleshooting guide
2022 self study materials for Zhejiang computer level III network and security technology examination (1) (updated on 2.28)
C asynchronous programming (async and await) and asynchronous method synchronous invocation
[GIS tutorial] land use transfer matrix
Main business objects of pupanvr record (5)