20.有效的括号
代码如下:
class Solution {
public:
bool isValid(string s) { //说实话这题还是有难度的
int size = s.size();
stack<char> temp;
if (size % 2) {
return false;
}
for (int i = 0; i < size; i++) {
if (s[i] == '(') {
temp.push(')');
} else if (s[i] == '[') {
temp.push(']');
} else if (s[i] == '{') {
temp.push('}');
} else if (temp.empty() || s[i] != temp.top()) {
return false;
} else {
temp.pop();
}
}
return temp.empty();
}
};
完整代码如下:
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string s) { //说实话这题还是有难度的
int size = s.size();
stack<char> temp;
if (size % 2) {
return false;
}
for (int i = 0; i < size; i++) {
if (s[i] == '(') {
temp.push(')');
} else if (s[i] == '[') {
temp.push(']');
} else if (s[i] == '{') {
temp.push('}');
} else if (temp.empty() || s[i] != temp.top()) {
return false;
} else {
temp.pop();
}
}
return temp.empty();
}
};
int main() {
Solution sol;
string s = "{[[{";
bool ans = sol.isValid(s);
cout << ans << endl;
stack<char> s1;
stack<char> s2;
s1.push('a');
s1.push('b');
s2.push('a');
s2.push('b');
if (s1 == s2) {
cout << true << endl;
}
}
1047. 删除字符串中的所有相邻重复项
代码如下:
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
string ans;
for (int i = s.size() - 1; i >= 0; i--) {
if (st.empty()) {
st.push(s[i]);
} else {
if (s[i] == st.top()) {
st.pop();
} else {
st.push(s[i]);
}
}
}
while (!st.empty()) {
ans += st.top();
st.pop();
}
return ans;
}
};
完整代码如下:
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
string ans;
for (int i = s.size() - 1; i >= 0; i--) {
if (st.empty()) {
st.push(s[i]);
} else {
if (s[i] == st.top()) {
st.pop();
} else {
st.push(s[i]);
}
}
}
while (!st.empty()) {
ans += st.top();
st.pop();
}
return ans;
}
};
int main() {
Solution sol;
string s = "abbaca";
string ans = sol.removeDuplicates(s);
cout << ans << endl;
}
150.逆波兰表达式求值
代码如下:
class Solution {
public:
int evalRPN(vector<string>& tokens) { //用栈来存储数字,如果遍历到了运算符,就把栈中的数字取出两个,然后进行运算,再将运算结果给栈。
stack<long long> temp; //力扣要求用long long数据类型
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { //注意这里是双引号,因为是字符串
long long nums1 = temp.top();
temp.pop();
long long nums2 = temp.top();
temp.pop();
if (tokens[i] == "+") {
temp.push(nums2 + nums1);
} else if (tokens[i] == "-") {
temp.push(nums2 - nums1);
} else if (tokens[i] == "*") {
temp.push(nums2 * nums1);
} else {
temp.push(nums2 / nums1);
}
} else {
temp.push(stoll(tokens[i])); //stoll函数用来将字符串转成long long
}
}
return temp.top();
}
};
完整代码如下:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) { //用栈来存储数字,如果遍历到了运算符,就把栈中的数字取出两个,然后进行运算,再将运算结果给栈。
stack<long long> temp; //力扣要求用long long数据类型
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { //注意这里是双引号,因为是字符串
long long nums1 = temp.top();
temp.pop();
long long nums2 = temp.top();
temp.pop();
if (tokens[i] == "+") {
temp.push(nums2 + nums1);
} else if (tokens[i] == "-") {
temp.push(nums2 - nums1);
} else if (tokens[i] == "*") {
temp.push(nums2 * nums1);
} else {
temp.push(nums2 / nums1);
}
} else {
temp.push(stoll(tokens[i])); //stoll函数用来将字符串转成long long
}
}
return temp.top();
}
};
int main() {
vector<string> tokens;
tokens.push_back("4");
tokens.push_back("13");
tokens.push_back("5");
tokens.push_back("/");
tokens.push_back("+");
Solution sol;
int ans = sol.evalRPN(tokens);
cout << ans << endl;
}
今毕。