Journey of LeetCode|DAY 10
Preface
This is a new day to continue my stack and queue journey.
Learn something new and keep reviewing what I learnt before.
1. Valid Parentheses
LeetCode Link: 20. Valid Parentheses
Given a string s containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Analysis and Solution
Stack
LeetCode C++ as followings Stack
class Solution {
public:
bool isValid(string s) {
if (s.size() % 2 != 0) return false; // if length of string is odd number. it cannot matched finally.
stack<char> st;//define a stack to store s
for (int i = 0; i < s.size(); i++) {//traverse s
if (s[i] == '(') st.push(')');//push the matched bracket in the stack.
else if (s[i] == '{') st.push('}');//same as before
else if (s[i] == '[') st.push(']');//same as before
else if (st.empty() || st.top() != s[i]) return false;//stack is empty when traverse s,whcih means there is no matched bracket for former bracket. false situation 1; top one in the stack is not what we want to match with former one. false situation 2.
else st.pop(); // st.top() is equal to s[i];pop the bracket we pushed in before
}
// stack is not empty after traversing,which means there exists unlucky items without partner to match with.false situation 3
return st.empty();//or return ture.
}
};
2.Remove All Adjacent Duplicates In String
LeetCode Link: 1047. Remove All Adjacent Duplicates In String
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Analysis and Solution
Stack
LeetCode C++ as followings Stack
class Solution {
public:
string removeDuplicates(string S) {
stack<char> st;//define a stack to store S
for (char s : S) {//traverse S
if (st.empty() || s != st.top()) {//stack is empty or top of the stack is not equal to string s.
st.push(s);//push it in the stack
} else {
st.pop(); // s is equal to top of the stack,which means Adjacent Duplicate one should be remove. so pop it from stack.
}
}
string result = "";//define what we want finally
while (!st.empty()) { // stack is not empty eventually
result += st.top();//save the items of the stack in the result. pop items one by one from the stack
st.pop();//pop items one by one from the stack
}
reverse (result.begin(), result.end()); // cause FILO . reverse result is what we want eventually
return result;//show the outcome
}
};
3.Evaluate Reverse Polish Notation
LeetCode Link: 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, and /. Each operand may be an integer or another expression.
Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Analysis and Solution
Stack
LeetCode C++ as followings Stack
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long> st; //longlong because of dataset of the leetcode have been updated
for (int i = 0; i < tokens.size(); i++) {//traverse tokens
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {//meet the Arithmetic symbols,pop the top two items of the stack and make calculation with two
long long num1 = st.top();
st.pop();//pop num1
long long num2 = st.top();
st.pop();//pop num2
if (tokens[i] == "+") st.push(num2 + num1);//make calculation with two,and push the result in the stack.
if (tokens[i] == "-") st.push(num2 - num1);//same as before
if (tokens[i] == "*") st.push(num2 * num1);//same as before
if (tokens[i] == "/") st.push(num2 / num1);//same as before
} else {
st.push(stoll(tokens[i]));//to be reviewed later
//std::stoll(): This function converts a string, provided as an argument in the function call, to long long int. It parses str interpreting its content as an integral number of the specified base, which is returned as a value of type long long int.
}
}
int result = st.top();//the final result.
st.pop(); // pop the result
return result;
}
};