//栈和队列
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//动态数组
template<typename T>
class Array
{
private:
T* data;
int capacity;
int size;
public:
Array():Array(5){}
Array(int capacity){data=new T[capacity]; size =0;this->capacity=capacity;}
int getsize(){return size;}
int getCapacity(){return capacity;}
bool isEmpty(){return size==0;}
void addLast(T e){add(size,e);}
void addFirst(T e){add(0,e);}
void add(int index,T e){
if(size==capacity) resize(2*capacity);
if(index<0||index>size)
throw "Add failed.Require index>=0 and index<=size.";
for(int i=size-1;i>=index;i--)
*(data+i+1)=*(data+i);
*(data+index)=e;
size++;
}
T get(int index){
if(index<0 || index>=size)throw "Add failed. Index is illegal.";
return *(data+index);
}
T getLast(){ return get(size-1);}
T getFirst(){ return get(0);}
void set(int index,T e){
if(index<0 || index>=size)throw "Add failed. Index is illegal.";
*(data+index)=e;
}
bool contains(T e){
for(int i=0;i<size;i++)
if(*(data+i)==e) return true;
return false;
}
int find(int e){
for(int i=0;i<size;i++)
if(*(data+i)==e) return i;
return -1;
}
T remove(int index){
if(index<0 || index>=size)throw "Add failed. Index is illegal.";
T ret=*(data+index);
for(int i=index+1;i<size;i++) *(data+i-1)=*(data+i);
size--;
if(size==capacity/4 && capacity/2!=0) resize(capacity/2);
return ret;
}
T removeFirst(){remove(0);}
T removeLast(){remove(size-1);}
bool removeElement(T e){
int index=find(e);
if(index){return remove(index);}
else return false;
}
void print(){
for(int i=0;i<size;i++)
if(i==0) cout<<"{"<<*(data+i)<<",";
else if(i==size-1) cout<<*(data+i)<<"}"<<endl;
else cout<<*(data+i)<<",";
}
private:
void resize(int newCapacity){
T* newData=new T[newCapacity];
for(int i=0;i<size;i++)
*(newData+i)=*(data+i);
data=newData;
capacity=newCapacity;
}
};
void TestArray()
{
Array<int> aa;
for(int i=1;i<10;i++)
aa.addLast(i);
aa.print();
}
//栈的基本实现
template <typename T >
class ArrayStack
{
public:
Array<T> array;
public:
ArrayStack(){}
int getSize(){return array.getsize();}
bool isEmpty(){return array.isEmpty();}
int getCapacity(){return array.getCapacity();}
void push(T e){array.addLast(e);}
T pop(){return array.removeLast();}
T top(){ return array.getLast();}
string toString(){
string res;
res.append("Stack: [");
for(int i=0;i<array.getsize();i++){
stringstream ss;
ss<<array.get(i);
res.append(ss.str());
if(i!=array.getsize()-1)
res.append(",");
}
res.append("] top\n");
return res;
}
};
void TestArrayStack()
{
ArrayStack<int> stack;
for(int i=0;i<5;i++){
stack.push(i);
cout<<stack.toString();
}
stack.pop();
cout<<stack.toString();
}
//数组队列
template <typename T >
class Queue
{
public:
Array<T> array;
public:
Queue(){}
int size(){ return array.getSize();}
bool empty(){ return array.isEmpty();}
void push(T e){ array.addLast(e);}
void pop(){ array.removeFirst();}
T front(){ array.getFirst();}
string toString(){
string res;
res.append("Queue: front [");
for(int i=0;i<array.getsize();i++){
stringstream ss;
ss<<array.get(i);
res.append(ss.str());
if(i!=array.getsize()-1)
res.append(",");
}
res.append("] tail\n");
return res;
}
};
void TestQueue()
{
Queue<int> que;
for(int i=0;i<10;i++){
que.push(i);
cout<<que.toString();
if(i%3==2){
que.pop();
cout<<que.toString();
}
}
}
//循环队列
template <typename T>
class LoopQueue
{
public:
T* data;
int front,tail;
int size;
int capacity;
public:
LoopQueue():LoopQueue(10){}
LoopQueue(int capacity){
data=new T[capacity+1];
front=0;tail=0;size =0;
this->capacity=capacity+1;
}
int getCapacity(){ return capacity-1;}
bool isEmpty(){ return size==0;}
int getsize(){ return size;}
void push(T e){
if((tail+1)%capacity == front)
resize(getCapacity()*2);
*(data+tail)=e;
tail=(tail+1)%capacity;
size++;
}
T pop(){
if(isEmpty()) throw "Queue is empty.";
T ret=*(data+front);
//*(data+front)=NULL;
front=(front+1)%capacity;
size--;
if(size==getCapacity()/4 && getCapacity()/2!=0)
resize(getCapacity()/2);
return ret;
}
T getfront(){
if(isEmpty()) throw "Queue is empty.";
return *(data+front);
}
string toString(){
string res;
stringstream ss;
res.append("LoopQueue: size =");
ss<<size;
res.append(ss.str());
ss.str("");
res.append(" capacity =");
ss<<getCapacity();
res.append(ss.str());
ss.str("");
res.append("\nfront [");
for(int i=front;i!=tail;i=(i+1)%capacity){
ss<<*(data+i);
res.append(ss.str());
ss.str("");
if((i+1)%capacity != tail)
res.append(",");
}
res.append("] tail\n");
return res;
}
public:
void resize(int newCapacity){
T* newData=new T[newCapacity+1];
for(int i=0;i<size;i++)
*(newData+i)=*(data+(front+i)%capacity);
delete[] data;
data=newData;
front=0;
tail=size;
capacity=newCapacity+1;
}
};
void TestLoopQueue()
{
LoopQueue<int> que;
for(int i=0;i<10;i++){
que.push(i);
cout<<que.toString();
if(i%3==2){
que.pop();
cout<<que.toString();
}
}
}
int main(int argc, char *argv[])
{
//TestArrayStack();
//cout<<endl;
//TestQueue();
TestLoopQueue();
return 0;
}
20. 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
示例 3:
输入:s = "(]"
输出:false
示例 4:
输入:s = "([)]"
输出:false
示例 5:
输入:s = "{[]}"
输出:true
示例 6:
输入:s = "]"
输出:false
提示:
1 <= s.length <= 104
s 仅由括号 '()[]{}' 组成
class Solution {
public:
bool isValid(string s) {
stack<char> stack;
for(int i=0;i<s.size();i++){
char c=s[i];
if(c=='('||c=='['||c=='{')
stack.push(c);
else if(stack.empty()) return false;
else if(c==')' && stack.top()!='(')
return false;
else if(c==']' && stack.top()!='[')
return false;
else if(c=='}' && stack.top()!='{')
return false;
else stack.pop();
}
return stack.empty();
}
};