您的位置:首页 > 编程语言 > C语言/C++

C++实现一个栈,出栈,入栈,返回最小值 时间复杂度为O(1),查找字符串中第一个重复出现的字符

2017-07-19 15:33 579 查看
#include<iostream>

#include<stack>

using namespace std;

template <class T>

class Stack

{

public:
void push(int d)
{
s.push(d);
if (s.size()==1)
{
min.push(d);
}
else
if (s.top() <= min.top())
{
min.push(d);
}
}
void pop()
{
if (!s.empyt())
{
if (s.top() == min.top())
min.pop();
s.pop();
}
}
T& Min()
{
return min.top();
}
private:
stack<int> min;
stack<int> s;

};

char findsecondchar(char string[])

{
char s[300] = { 0 };
int i = 0;
while (string[i] != '\0')
{
int temp = string[i];
++s[temp];
if (s[temp] == 2)
{
return string[i];
}
i++;

}
return 0;

}

int main()

{
char string[] = { "abcdffr" };
cout<<findsecondchar(string)<<endl;
Stack<int> s;
s.push(6);
s.push(2);
s.push(3);
s.push(8);
cout << s.Min() << endl;

system("pause");
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐