Leetcode155--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.

Main topic

Design a stack that supports push, pop, top, and returns the smallest element within a constant time.

Difficulty factor : Easy

Analysis

This is not difficult to write, but to let Leetcode can run your code, than escorts This problem is still laborious. For example, at first I used a vector and the result was a run-time error. Later, it was a question of leetcode. As far as my implementation code is concerned, the initial value of the _capacity is changed to 10, the program runs normally, it is changed to 100, and the program runs in error. After the completion, found that their implementation and Leetcode give the idea is consistent, also let me quite gratified, although took a little time.

Realize

Template <class t>class Vector {public:vector (): _capacity (1), _size (0), _pt (NULL) {_pt = (t*) malloc (_capac    Ity*sizeof (T));    } ~vector () {free (_PT);            } void Push_back (T-t) {if (_size >= _capacity) {_capacity = _capacity * 5;            t* ptmp = (t*) realloc (_pt, _capacity*sizeof (T));            if (ptmp) {_pt = ptmp;        }} _pt[_size] = t;    _size++;        } void Pop_back () {if (_size > 0) {--_size;    }} t& operator[] (size_t POS) {return _pt[pos];    } const t& operator[] (size_t p) const {return _pt[p];        } int Erase (int i) {for (int k = i; k < _size-1; ++k) {_pt[k] = _pt[k+1];        } _size = _size-1;    return i;            } void Insert (int pos, T t) {if (_size >= _capacity) {_capacity = _capacity * 5;      t* ptmp = (t*) realloc (_pt, _capacity*sizeof (T));      if (ptmp) {_pt = ptmp;        }} for (int i = _size; i > pos;-i) {_pt[i] = _pt[i-1];        } _pt[pos] = t;    _size + = 1;    } int Size () const {return _size;    } T back () const{return _pt[_size-1];    } T Front () const {return _pt[0];    }private:t* _pt;    int _size; int _capacity;};        Class Minstack {public:void push (int x) {v.push_back (x);        if (ov.size () = = 0) {ov.push_back (x);    } else if (x <= ov.back ()) ov.push_back (x);        } void Pop () {if (v.size () <= 0) return;        int val = V.back ();        V.pop_back ();        if (val = = Ov.back ()) {ov.pop_back ();    }} int Top () {return v.back ();    } int Getmin () {return ov.back ();    } const vector<int>& Getv () const {return v;    }private:vector<int> v; Vector<int> ov;}; 

Leetcode155--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.