算法学习第十一天|20.有效的括号,1047.删除字符串中的所有相邻重复项,150.逆波兰表达式求值

目录

前言

一、有效括号(力扣20)

二、删除字符串中的所有相邻重复元素(力扣1047)

三、逆波兰表达式(力扣150)


前言

学习栈的操作


一、有效括号(力扣20)

实现思想:每当扫描到字符串中  “(”  “[”  “{”  时,将  “)”  “]”  “}”,加入到栈中,接着扫描到字符串中  “)”  “]”  “}” 时,和栈顶的元素对比,如果相同说明字符串括号匹配,反之不匹配。  

有三种错误的情况:

1.字符串里左方向的括号多余了,所以不匹配

2.括号没有多余,但是括号的类型没有匹配上

3.字符串里右方向的括号多余了,所以不匹配

1.力扣代码

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        for (int i = 0; i < s.size(); i++){
            if (s[i] == '(') st.push(')');
            else if (s[i] == '[') st.push(']');
            else if (s[i] == '{') st.push('}');
            else if (st.empty() || st.top() != s[i]){
                return false;
            }else{
                st.pop();
            }
        } 
        return st.empty();
    }
};

2.自行实现

#include <iostream>
#include <stack> 

using namespace std;

bool function(string s){
	if (s.size()%2 != 0) return false;
	stack<char> sd;
	for (int i = 0; i < s.size(); i++){
		if (s[i] == '(') sd.push(')');
		else if (s[i] == '[') sd.push(']');
		else if (s[i] == '{') sd.push('}');
		else if (sd.empty() || s[i] != sd.top()){
			return false;
		}else{
			sd.pop();
		}
	} 
	return sd.empty();
}

int main(void){
	string s1 = "()";
	string s2 = "()[]{}";
	string s3 = "(]";
	string s4 = "([)]";
	string s5 = "{[]}";
	cout << function(s1) << endl; 
	cout << function(s2) << endl;
	cout << function(s3) << endl; 
	cout << function(s4) << endl;
	cout << function(s5) << endl; 
	return 0;
} 

二、删除字符串中的所有相邻重复元素(力扣1047)

实现思想:每一次在将字符串中的字符串中的元素入栈之前,先检查栈顶元素和当前字符串中准备入栈的元素是否相同,如果相同则将栈顶元素出栈,否则将当前元素入栈。

1.力扣代码

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for (auto p : s){
            if (st.empty() || st.top() != p){
                st.push(p);
            }else{
                st.pop();
            }
        }
        string res = "";
        while(!st.empty()){
            res.push_back(st.top());
            st.pop();
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

2.自行实现

#include <iostream>
#include <stack> 
#include <algorithm>

using namespace std;

void function(string s){
	stack<char> st;
	for (auto p : s){
		if (st.empty() || p != st.top()){
			st.push(p);
		}else{
			st.pop();
		}
	}
	string res = "";
	while (!st.empty()) {
		res.push_back(st.top());
        st.pop();
    }
    reverse(res.begin(), res.end());
    cout << res;
}

int main(void){
	string s = "abbaca";
	function(s);
	return 0;
} 

三、逆波兰表达式(力扣150)

实现思想:根据逆波兰表达式的特性,只需要按顺序遍历逆波兰表达式的字符串即可。每一次如果遍历到的是数字,则将数字入栈。如果遇到的是运算符,则将栈顶的两个元素出栈,再将这两个元素的运算结果入栈

1.力扣代码

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<long long> st;
        for (int i = 0; i < tokens.size(); i++){
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"){
                long long num1 = st.top();
                st.pop();
                long long num2 = st.top();
                st.pop();
                if (tokens[i] == "+") st.push(num2 + num1);
                if (tokens[i] == "-") st.push(num2 - num1);
                if (tokens[i] == "*") st.push(num2 * num1);
                if (tokens[i] == "/") st.push(num2 / num1);
            }else{
                st.push(stoll(tokens[i]));
            }
        }
        long long result = st.top();
        st.pop();
        return result;
    }
};

2.自行实现

#include <iostream>
#include <stack>
#include <vector>

using namespace std;

void function(vector<string> tokens){
	stack<long long> st;
	for (int i = 0; i < tokens.size(); i++){
		if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"){
			long long num1 = st.top();
			st.pop();
			long long num2 = st.top();
			st.pop();
			if (tokens[i] == "+") st.push(num2 + num1);
			if (tokens[i] == "-") st.push(num2 - num1);
			if (tokens[i] == "*") st.push(num2 * num1);
			if (tokens[i] == "/") st.push(num2 / num1);
		}else{
			st.push(stoll(tokens[i]));
		}
	}
	int result = st.top();
	st.pop();
	cout << result;
}

int main(void){
	vector<string> tokens = {"4", "13", "5", "/", "+"};
	function(tokens);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值