In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how LeetCode realizes the push and pop sequence of the stack". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how LeetCode realizes the push and pop sequence of the stack".
Title Description
Enter two integer sequences. The first sequence indicates the push order of the stack. Please determine whether the second sequence may be the pop order of the stack. Assume that all the numbers pushed into the stack are unequal. For example, sequence 1, 2, 3, 4, 5 is the push sequence of a stack, sequence 4, 5, 3, 2, 1 is a pop sequence corresponding to the push sequence, but 4, 3, 5, 1, 2 cannot be the pop sequence of the push sequence. (Note that the lengths of these two sequences are equal.)
class Solution: def IsPopOrder(self, pushV, popV): if not pushV or not popV or len(pushV) != len(popV): return False stack = [] for pop in popV: #For each element in the pop-up sequence, if it is not at the top of the stack, then push it one by one from the push sequence until the top element is #Elements to be ejected. #If the push sequence is empty and the pop-up sequence is not found at the top of the stack, it means that the pop-up sequence and the push sequence cannot match. if not stack or stack[-1] != pop: while pushV: stack.append(pushV[0]) if pushV.pop(0) == pop: break if stack and stack[-1] == pop: stack.pop(-1) else: return False return True The above is "LeetCode how to achieve stack push, pop sequence" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.