带有getMin栈

来源:互联网 发布:java编程是不是简单点 编辑:程序博客网 时间:2024/04/29 19:40

带有getMin栈

#include<iostream>#include<stack>using namespace std;template<class T>class MinStack{public:    void Pop()    {        if (st.top() == minSt.top())        {            minSt.pop();        }        st.pop();    }    void Push(T data)    {        st.push(data);        if (minSt.empty() || data < minSt.top())        {            minSt.push(data);        }    }    T GetMin()    {        return minSt.top();    }private:    stack<T> st;    stack<T> minSt;};int main(){    MinStack<int> q;        q.Push(5);    q.Push(4);    q.Push(9);    q.Push(8);    q.Push(10);     q.Push(6);    q.Push(7);    q.Pop();    cout << q.GetMin() << endl;    q.Pop(); q.Pop();q.Push(3);    cout << q.GetMin()<< endl;    system("pause");}

字符串中第一次只出现两次的字符

#include<iostream>using namespace std;bool errorFlag = false;char getApearTwochar(const char *str, int len){    if (str == nullptr || len <= 0)        return 0;    int i = 0;    int count = 0;    for (; i < len; i++)    {        for (int j = i + 1; j < len; j++)        {            if (str[i] == str[j])            {                ++count;            }        }        if (count == 1)            break;        count = 0;    }    errorFlag = true;    return str[i];}int main(){    char *str = "abccddkiujlkajlkjjlakj";    int len = strlen(str);    char res = getApearTwochar(str, len);    cout << res<< endl;    system("pause");}
原创粉丝点击