Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5ae2f8

Browse files
committed
Add C++ solutions for Chapter 7 (Stacks)
1 parent 502f5bd commit d5ae2f8

6 files changed

+195
-0
lines changed

cpp/Stacks/evaluate_expression.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <string>
2+
#include <stack>
3+
4+
int evaluateExpression(std::string s) {
5+
std::stack<int> stack;
6+
int currNum = 0, sign = 1, res = 0;
7+
for (char c : s) {
8+
if (isdigit(c)) {
9+
currNum = currNum * 10 + (c - '0');
10+
}
11+
// If the current character is an operator, add 'currNum' to
12+
// the result after multiplying it by its sign.
13+
else if (c == '+' || c == '-') {
14+
res += currNum * sign;
15+
// Update the sign and reset 'currNum'.
16+
sign = (c == '-') ? -1 : 1;
17+
currNum = 0;
18+
}
19+
// If the current character is an opening parenthesis, a new
20+
// nested expression is starting.
21+
else if (c == '(') {
22+
// Save the current 'res' and 'sign' values by pushing them
23+
// onto the stack, then reset their values to start
24+
// calculating the new nested expression.
25+
stack.push(res);
26+
stack.push(sign);
27+
res = 0;
28+
sign = 1;
29+
}
30+
// If the current character is a closing parenthesis, a nested
31+
// expression has ended.
32+
else if (c == ')') {
33+
// Finalize the result of the current nested expression.
34+
res += sign * currNum;
35+
// Apply the sign of the current nested expression's result
36+
// before adding this result to the result of the outer
37+
// expression.
38+
int prevSign = stack.top(); stack.pop();
39+
res *= prevSign;
40+
int prevRes = stack.top(); stack.pop();
41+
res += prevRes;
42+
currNum = 0;
43+
}
44+
}
45+
// Finalize the result of the overall expression.
46+
return res + currNum * sign;
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <vector>
2+
#include <optional>
3+
4+
class Queue {
5+
public:
6+
Queue() {}
7+
8+
void enqueue(int x) {
9+
enqueueStack.push_back(x);
10+
}
11+
12+
void transferEnqueueToDequeue() {
13+
// If the dequeue stack is empty, push all elements from the enqueue stack
14+
// onto the dequeue stack. This ensures the top of the dequeue stack
15+
// contains the most recent value.
16+
if (dequeueStack.empty()) {
17+
while (!enqueueStack.empty()) {
18+
dequeueStack.push_back(enqueueStack.back());
19+
enqueueStack.pop_back();
20+
}
21+
}
22+
}
23+
24+
std::optional<int> dequeue() {
25+
transferEnqueueToDequeue();
26+
// Pop and return the value at the top of the dequeue stack.
27+
if (!dequeueStack.empty()) {
28+
int val = dequeueStack.back();
29+
dequeueStack.pop_back();
30+
return val;
31+
}
32+
return std::nullopt;
33+
}
34+
35+
std::optional<int> peek() {
36+
transferEnqueueToDequeue();
37+
// Return the value at the top of the dequeue stack without removing it.
38+
if (!dequeueStack.empty()) {
39+
return dequeueStack.back();
40+
}
41+
return std::nullopt;
42+
}
43+
44+
private:
45+
std::vector<int> enqueueStack;
46+
std::vector<int> dequeueStack;
47+
48+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <vector>
2+
#include <deque>
3+
4+
std::vector<int> maximumsOfSlidingWindow(std::vector<int>& nums, int k) {
5+
std::vector<int> res;
6+
std::deque<std::pair<int, int>> dq;
7+
int left = 0, right = 0;
8+
while (right < nums.size()) {
9+
// 1) Ensure the values of the deque maintain a monotonic decreasing order
10+
// by removing candidates <= the current candidate.
11+
while (!dq.empty() && dq.back().first <= nums[right]) {
12+
dq.pop_back();
13+
}
14+
// 2) Add the current candidate.
15+
dq.push_back(std::make_pair(nums[right], right));
16+
// If the window is of length 'k', record the maximum of the window.
17+
if (right - left + 1 == k) {
18+
// 3) Remove values whose indexes occur outside the window.
19+
if (!dq.empty() && dq.front().second < left) {
20+
dq.pop_front();
21+
}
22+
// The maximum value of this window is the leftmost value in the
23+
// deque.
24+
res.push_back(dq.front().first);
25+
// Slide the window by advancing both 'left' and 'right'. The right
26+
// pointer always gets advanced so we just need to advance 'left'.
27+
left++;
28+
}
29+
right++;
30+
}
31+
return res;
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <vector>
2+
3+
std::vector<int> nextLargestNumberToTheRight(std::vector<int>& nums) {
4+
std::vector<int> res(nums.size(), 0);
5+
std::vector<int> stack;
6+
// Find the next largest number of each element, starting with the
7+
// rightmost element.
8+
for (int i = nums.size() - 1; i >= 0; i--) {
9+
// Pop values from the top of the stack until the current
10+
// value's next largest number is at the top.
11+
while (!stack.empty() && stack.back() <= nums[i]) {
12+
stack.pop_back();
13+
}
14+
// Record the current value's next largest number, which is at
15+
// the top of the stack. If the stack is empty, record -1.
16+
res[i] = !stack.empty() ? stack.back() : -1;
17+
stack.push_back(nums[i]);
18+
}
19+
return res;
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <string>
2+
#include <vector>
3+
4+
std::string repeatedRemovalOfAdjacentDuplicates(std::string s) {
5+
std::vector<char> stack;
6+
for (char c : s) {
7+
// If the current character is the same as the top character on the stack,
8+
// a pair of adjacent duplicates has been formed. So, pop the top character
9+
// from the stack.
10+
if (!stack.empty() && c == stack.back()) {
11+
stack.pop_back();
12+
}
13+
// Otherwise, push the current character onto the stack.
14+
else {
15+
stack.push_back(c);
16+
}
17+
}
18+
// Return the remaining characters as a string.
19+
return std::string(stack.begin(), stack.end());
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <string>
2+
#include <unordered_map>
3+
#include <vector>
4+
5+
bool validParenthesisExpression(std::string s) {
6+
std::unordered_map<char, char> parenthesesMap = {{'(', ')'}, {'{', '}'}, {'[', ']'}};
7+
std::vector<char> stack;
8+
for (char c : s) {
9+
// If the current character is an opening parenthesis, push it
10+
// onto the stack.
11+
if (parenthesesMap.find(c) != parenthesesMap.end()) {
12+
stack.push_back(c);
13+
}
14+
// If the current character is a closing parenthesis, check if
15+
// it closes the opening parenthesis at the top of the stack.
16+
else {
17+
if (!stack.empty() && parenthesesMap[stack.back()] == c) {
18+
stack.pop_back();
19+
}
20+
else {
21+
return false;
22+
}
23+
}
24+
}
25+
// If the stack is empty, all opening parentheses were successfully
26+
// closed.
27+
return stack.empty();
28+
}

0 commit comments

Comments
 (0)