Java for Leetcode 155 Min Stack

Source: Internet
Author: User

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Push (x)-push element x onto stack.
Pop ()--Removes the element on top of the stack.
Top ()--Get the top element.
Getmin ()--Retrieve The minimum element in the stack.

Problem Solving Ideas:

The topic is the Java language Programming-Basic article of the original problem, modified can be, Java implementation is as follows:

public class Minstack {private int[] elements;private int size;public minstack () {elements = new int[16];} public void push (int x) {if (size >= elements.length) {int[] temp = new int[elements.length * 2]; System.arraycopy (elements, 0, temp, 0, elements.length); elements = temp;} elements[size++] = x;} public void Pop () {size--;} public int Top () {return elements[size-1];} public int getmin () {int min=integer.max_value;for (int i=0;i<size;i++) if (Min>elements[i]) min=elements[i]; return min;}}

Java for Leetcode 155 Min Stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.