20. 有效的括号
class Solution {
stack<char> sta;
public:
bool isValid(string s) {
if(s.size() %2 !=0){ //规则 成对出现
return false;
}
for(auto &c : s){
if(c == '(') { //出现前一半 入栈后一半
sta.push(')');
}
else if(c == '{'){
sta.push('}');
}
else if( c == '['){
sta.push(']');
}else if(sta.empty() || sta.top() != c){ //匹配右边时,栈空--栈顶的元素不为c 错误
return false;
}else{
sta.pop(); //相同则 出栈
}
}
return sta.empty(); //重点 返回的是栈不为空的部分
}
};
1047. 删除字符串中的所有相邻重复项
class Solution {
public:
string removeDuplicates(string s) {
stack<char>res;
for(auto & c:s){
if(res.empty()|| c != res.top()){
res.push(c);
}else{
res.pop();
}
}
string result = "";
while(!res.empty()){
result += res.top();
res.pop();
}
reverse(result.begin(), result.end()); //将出栈的顺序翻转
return result;
}
};
150. 逆波兰表达式求值
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long> res;
for(int i =0 ; i< tokens.size(); i++){
if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"){
long long num1 = res.top();
res.pop();
long long num2 = res.top();
res.pop();
if(tokens[i] == "+") {
res.push(num1 + num2);
}
if(tokens[i] == "-"){
res.push(num2 - num1);
}
if(tokens[i] == "*"){
res.push(num2*num1);
}
if(tokens[i] == "/"){
res.push(num2 / num1);
}
}else{
res.push(stoll(tokens[i]));
}
}
int result = res.top();
res.pop();
return result;
}
};
排序 快排和归并排序,KMPnext数组
class sortMethod {
int position(vector<int>& num, int left, int right) {
int base = num[right];
int i = left - 1;
for (int j = left; j <= right; j++) {
if (num[j] < base) {
i++;
swap(num[i], num[j]);
}
}
swap(num[i + 1], num[right]);
return i + 1;
}
void quickSort(vector<int>& num, int left, int right) {
if (left < right) {
int pos = position(num, left, right);
quickSort(num, left, pos - 1);
quickSort(num, pos + 1, right);
}
}
void mergeAll(vector<int>& num, vector<int>& L, int leftNum, vector<int>& R, int rightNum) {
int i = 0, j = 0, k = 0;
while (i < leftNum && j < rightNum) {
if (L[i] <= R[j]) {
num[k++] = L[i++];
}
else {
num[k++] = R[j++];
}
}
while (i < leftNum) {
num[k++] = L[i++];
}
while (j < rightNum) {
num[k++] = R[j++];
}
}
void megerSort(vector<int>& num, int n) {
if (n < 2) return;
int mid = n / 2;
vector<int> L(mid);
vector<int>R(n - mid);
for(int i = 0; i< mid; i++)
{
L[i] = num[i];
}
for (int i = mid; i < n; i++) {
R[i - mid] = num[i];
}
megerSort(L, mid);
megerSort(R, n - mid);
mergeAll(num, L, mid, R, n - mid);
}
void getnext(int *next, string noddle) {
next[0] = -1;
int j = -1; //第一步 初始值为-1
for (int i = 1; i < noddle.size(); i++) { //第二步 i从1开始
while (j >= 0 && noddle[i] != noddle[j+1]) { //j+1
j = next[j];
}
while (noddle[i] == noddle[j+1]) { //i , j+1
j++;
}
next[i] = j; // 更新next数组
}
}
public:
void method_quick(vector<int>& num) {
int left = 0;
int right = num.size() - 1;
quickSort(num, 0, right);
cout << "快速排序:" << endl;
for (auto& c : num) {
cout << c << ",";
}
cout << endl;
}
void method_merge(vector<int>& num) {
megerSort(num, num.size());
cout << "归并排序:" << endl;
for (auto& c : num) {
cout << c << ",";
}
cout << endl;
}
};
int main() {
vector<int> num{ 1,7,4,7,43,8,9,54,87,3,6 };
vector<int> num2 = num;
sortMethod mysort;
mysort.method_merge(num);
mysort.method_quick(num2);
return 0;
}