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

Lintcode12 Min Stack solution problem solution

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Topic description]

Implement a stack with min () function, which will return the smallest number in the stack.

It should support push, pop and min operation all in O (1) cost.

Notice:min operation will never be called if there is no number in the stack.

Implement a stack with the min method that takes the minimum value, and the min method returns the minimum value in the current stack.

The stack you implement will support push,pop and min operations, and all operations will be completed in O (1) time.

Note: if there are no numbers in the stack, you cannot call the min method.

[topic link]

Http://www.lintcode.com/en/problem/min-stack/

[topic Analysis]

Two stack structures are used, one of which is the main normal stack, which meets the O (1) time requirements of pop () and push (), and the other, as an auxiliary minStack, is only stored in the integer of min. Min = Integer.parseInt (minStack.peek () .toString ())

When push (), if number > = min, then push to pop () on minStack, if number = = min, also pop from minStack

For the example in the question, the final stack is [2,3,1] and the minStack is [2,1].

[answer link]

Http://www.jiuzhang.com/solutions/min-stack/

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

Internet Technology

Wechat

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

12
Report