isValid-链接
实现的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 #include<iostream> #include<cmath> #include<cstdlib> #include<cstring> using namespace std; template<typename T> class Array{ private: T *data; //数组名 int size; //数组中元素的个数 int Capacity; //数组容量 public: Array(int Capacity){ data=new T [Capacity]; this->size=0; this->Capacity=Capacity; } Array(){ data=new T [100000]; this->size=0; this->Capacity=100000; } //获得数组的元素个数 int getSize(){ return size; } //判断数组是否为空 bool isEmpty(){ return size==0; } //获得数组的容量 int getArray(){ return Capacity; } //在数组中插入一个元素 void add(int index,T elem){ if(index<0||index>Capacity){ cout<<"插入的位置有误"<<endl; return ; } if(size>=Capacity){ resize(Capacity*2); } for(int i=size-1;i>=index;i--){ data[i+1]=data[i]; } data[index]=elem; size++; } //在数组的最后一个位置添加一个新元素elem; void addLast(T elem){ data[size]=elem; size++; } //获取index位置的数组元素 T get(int index){ if(index<0||index>=size){ cout<<"您输入的位置有误"; return -1; } return data[index]; } //在index位置处修改数组元素的值 void set(int index,T elem){ if(index<0||index>=size){ cout<<"您输入的位置有误"; return ; } data[index]=elem; } //查找数组中是否有元素e bool contains(T e){ for(int i=0;i<size;i++){ if(data[i]==e) return true; } return false; } //查找数组中元素e的索引index int find(T e){ for(int i=0;i<size;i++){ if(data[i]==e) return i; } return -1; } //删除数组中索引为index位置的元素,并且返回被删除的元素的值 T remove(int index){ if(index<0||index>=size){ return -1; } 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; } //改变数组的容量 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 PrintArray(){ for(int i=0;i<size;i++) cout<<data[i]<<" "; cout<<endl; } }; template<typename T> class ArrayStack{ private: Array<T> *arr; public: ArrayStack(int Capacity){ arr=new Array<T>[Capacity]; } ArrayStack(){ arr=new Array<T>(); } //入栈 void push(T e){ arr->addLast(e); } //出栈,返回弹出的元素T T pop(){ return arr->remove(arr->getSize()-1); } //判断栈是否为空 bool isEmpty(){ return arr->isEmpty(); } //查看栈顶元素 T peek(){ return arr->get(arr->getSize()-1); } //获得栈中的元素个数 int getSize(){ return arr->getSize(); } ~ArrayStack(){ delete []arr; arr= nullptr; } }; class Solution { public: bool isValid(string s) { ArrayStack<char> *arr= new ArrayStack<char>(); for(int i=0;s[i]!='\0';i++){ if(s[i]=='{'||s[i]=='['||s[i]=='('){ arr->push(s[i]); } else{ if(arr->isEmpty()) return false; char a=arr->pop(); if(s[i]=='}'&&a!='{') return false; else if(s[i]==']'&&a!='[') return false; else if(s[i]==')'&&a!='(') return false; } } if(arr->isEmpty()) return true; else return false; } };
自己写的栈(基于一个自己写的Array的动态数组完成实现)