In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how golang leetcode skills to achieve stacking dishes, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Pile up plates. Imagine a pile of plates, which may fall down if they are too high. Therefore, in real life, when the plates are piled up to a certain height, we will pile up another pile of plates. Please implement the data structure SetOfStacks to simulate this behavior. The SetOfStacks should consist of multiple stacks and create a new stack when the previous stack is full. In addition, SetOfStacks.push () and SetOfStacks.pop () should operate in the same way as regular stacks (that is, the value returned by pop () should be the same as when there is only one stack). Advanced: implement a popAt (int index) method that performs pop operations according to the specified substack.
When a stack is empty, you should delete the stack. When there are no elements in the stack or the stack does not exist, pop,popAt should return-1.
Example 1:
Enter:
["StackOfPlates", "push", "push", "popAt", "pop", "pop"]
[[1], [1], [2], [1], [], []]
Output:
[null, 2, 1,-1]
Example 2:
Enter:
["StackOfPlates", "push", "popAt", "popAt", "popAt"]
[[2], [1], [2], [3], [0], [0], [0]]
Output:
[null, 2, 1, 3]
Problem-solving ideas
1, it's not complicated here, it's just replacing one stack with multiple stacks.
2. It should be noted that the case where the input cap is 0 requires special handling.
3. If the last element of the current stack leaves the stack, you need to delete the stack.
4. If the previous stack is full, you need to create a new stack.
Code implementation
Type StackOfPlates struct {cap int data [] [] int}
Func Constructor (cap int) StackOfPlates {return StackOfPlates {cap:cap}}
Func (this * StackOfPlates) Push (val int) {if this.cap==0 {return} i:=len (this.data) if iComple0 | | len (this.data [I-1]) = = this.cap {this.data=append (this.data, [] int {val})} else {this.data [I-1] = append (this.data [I-1], val)}}
Func (this * StackOfPlates) Pop () int {lvv = len (this.data) if lager 0 {return-1} l1:=len (this.data [l-1]) val:= this.data [l-1] [L1-1] if L1room1 {this.data=this.data [: l-1:l-1]} else {this.dat a [l-1] = this.data [l-1] [: L1-1:l1-1]} return val}
Func (this * StackOfPlates) PopAt (index int) int {l:=len (this.data) if index > lmur1 {return-1} l1:=len (this.data [index.data] val:=this.data [index] [L1-1] if L1 index1 {this.data=append (this.data [: index:index], this.data [index + 1:])} else {this.data [index] = this.data [index] [: L1-1:l1-1]} return val
/ * Your StackOfPlates object will be instantiated and called as such: * obj: = Constructor (cap); * obj.Push (val); * param_2: = obj.Pop (); * param_3: = obj.PopAt (index); * / above is all the contents of the article "how to stack dishes with golang leetcode skills". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.