Skip to content

Commit 6c59f66

Browse files
committed
Added the Chaper Stack and Queue
1 parent d863ff9 commit 6c59f66

18 files changed

Lines changed: 336 additions & 6 deletions

‎book.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"gitbook": "2.x.x",
3-
"plugins": ["mathjax", "superscript", "github", "codesnippet"],
3+
"plugins": ["mathjax", "superscript", "github", "codesnippet", "image-captions"],
44
"pluginsConfig": {
55
"github": {
66
"url": "https://www.github.com/soulmachine/algorithm-essentials"

‎cpp/SUMMARY.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959
* [Simplify Path](string/simplify-path.md)
6060
* [Length of Last Word](string/length-of-last-word.md)
6161
* [栈和队列](stack-and-queue/README.md)
62-
* [栈](stack-and-queue/stack.md)
63-
* [队列](stack-and-queue/queue.md)
62+
* [栈](stack-and-queue/stack/README.md)
63+
* [Valid Parentheses](stack-and-queue/stack/valid-parentheses.md)
64+
* [Longest Valid Parentheses](stack-and-queue/stack/longest-valid-parentheses.md)
65+
* [Largest Rectangle in Histogram](stack-and-queue/stack/largest-rectangle-in-histogram.md)
66+
* [Evaluate Reverse Polish Notation](stack-and-queue/stack/evaluate-reverse-polish-notation.md)
67+
* [队列](stack-and-queue/queue/README.md)
6468
* [二叉树](binary-tree/README.md)
6569
* [二叉树的遍历](binary-tree/traversal.md)
6670
* [二叉树的构建](binary-tree/construction.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Evaluate Reverse Polish Notation
2+
// 递归,时间复杂度O(n),空间复杂度O(logn)
3+
class Solution {
4+
public:
5+
int evalRPN(vector<string> &tokens) {
6+
int x, y;
7+
auto token = tokens.back(); tokens.pop_back();
8+
if (is_operator(token)) {
9+
y = evalRPN(tokens);
10+
x = evalRPN(tokens);
11+
if (token[0] == '+') x += y;
12+
else if (token[0] == '-') x -= y;
13+
else if (token[0] == '*') x *= y;
14+
else x /= y;
15+
} else {
16+
size_t i;
17+
x = stoi(token, &i);
18+
}
19+
return x;
20+
}
21+
private:
22+
bool is_operator(const string &op) {
23+
return op.size() == 1 && string("+-*/").find(op) != string::npos;
24+
}
25+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Max Points on a Line
2+
// 迭代,时间复杂度O(n),空间复杂度O(logn)
3+
class Solution {
4+
public:
5+
int evalRPN(vector<string> &tokens) {
6+
stack<string> s;
7+
for (auto token : tokens) {
8+
if (!is_operator(token)) {
9+
s.push(token);
10+
} else {
11+
int y = stoi(s.top());
12+
s.pop();
13+
int x = stoi(s.top());
14+
s.pop();
15+
if (token[0] == '+') x += y;
16+
else if (token[0] == '-') x -= y;
17+
else if (token[0] == '*') x *= y;
18+
else x /= y;
19+
s.push(to_string(x));
20+
}
21+
}
22+
return stoi(s.top());
23+
}
24+
private:
25+
bool is_operator(const string &op) {
26+
return op.size() == 1 && string("+-*/").find(op) != string::npos;
27+
}
28+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Largest Rectangle in Histogram
2+
// 时间复杂度O(n),空间复杂度O(n)
3+
class Solution {
4+
public:
5+
int largestRectangleArea(vector<int> &height) {
6+
stack<int> s;
7+
height.push_back(0);
8+
int result = 0;
9+
for (int i = 0; i < height.size(); ) {
10+
if (s.empty() || height[i] > height[s.top()])
11+
s.push(i++);
12+
else {
13+
int tmp = s.top();
14+
s.pop();
15+
result = max(result,
16+
height[tmp] * (s.empty() ? i : i - s.top() - 1));
17+
}
18+
}
19+
return result;
20+
}
21+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Longest Valid Parenthese
2+
// 使用栈,时间复杂度O(n),空间复杂度O(n)
3+
class Solution {
4+
public:
5+
int longestValidParentheses(const string& s) {
6+
int max_len = 0, last = -1; // the position of the last ')'
7+
stack<int> lefts; // keep track of the positions of non-matching '('s
8+
9+
for (int i = 0; i < s.size(); ++i) {
10+
if (s[i] =='(') {
11+
lefts.push(i);
12+
} else {
13+
if (lefts.empty()) {
14+
// no matching left
15+
last = i;
16+
} else {
17+
// find a matching pair
18+
lefts.pop();
19+
if (lefts.empty()) {
20+
max_len = max(max_len, i-last);
21+
} else {
22+
max_len = max(max_len, i-lefts.top());
23+
}
24+
}
25+
}
26+
}
27+
return max_len;
28+
}
29+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Longest Valid Parenthese
2+
// 时间复杂度O(n),空间复杂度O(n)
3+
// @author 一只杰森(http://weibo.com/wjson)
4+
class Solution {
5+
public:
6+
int longestValidParentheses(const string& s) {
7+
vector<int> f(s.size(), 0);
8+
int ret = 0;
9+
for (int i = s.size() - 2; i >= 0; --i) {
10+
int match = i + f[i + 1] + 1;
11+
// case: "((...))"
12+
if (s[i] == '(' && match < s.size() && s[match] == ')') {
13+
f[i] = f[i + 1] + 2;
14+
// if a valid sequence exist afterwards "((...))()"
15+
if (match + 1 < s.size()) f[i] += f[match + 1];
16+
}
17+
ret = max(ret, f[i]);
18+
}
19+
return ret;
20+
}
21+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Longest Valid Parenthese
2+
// 两遍扫描,时间复杂度O(n),空间复杂度O(1)
3+
// @author 曹鹏(http://weibo.com/cpcs)
4+
class Solution {
5+
public:
6+
int longestValidParentheses(const string& s) {
7+
int answer = 0, depth = 0, start = -1;
8+
for (int i = 0; i < s.size(); ++i) {
9+
if (s[i] == '(') {
10+
++depth;
11+
} else {
12+
--depth;
13+
if (depth < 0) {
14+
start = i;
15+
depth = 0;
16+
} else if (depth == 0) {
17+
answer = max(answer, i - start);
18+
}
19+
}
20+
}
21+
22+
depth = 0;
23+
start = s.size();
24+
for (int i = s.size() - 1; i >= 0; --i) {
25+
if (s[i] == ')') {
26+
++depth;
27+
} else {
28+
--depth;
29+
if (depth < 0) {
30+
start = i;
31+
depth = 0;
32+
} else if (depth == 0) {
33+
answer = max(answer, start - i);
34+
}
35+
}
36+
}
37+
return answer;
38+
}
39+
};

‎cpp/code/valid-parentheses.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Valid Parentheses
2+
// 时间复杂度O(n),空间复杂度O(n)
3+
class Solution {
4+
public:
5+
bool isValid (string const& s) {
6+
string left = "([{";
7+
string right = ")]}";
8+
stack<char> stk;
9+
10+
for (auto c : s) {
11+
if (left.find(c) != string::npos) {
12+
stk.push (c);
13+
} else {
14+
if (stk.empty () || stk.top () != left[right.find (c)])
15+
return false;
16+
else
17+
stk.pop ();
18+
}
19+
}
20+
return stk.empty();
21+
}
22+
};

‎cpp/linear-list/array/gray-code.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Note:
4242

4343
方法1,最简单的方法,利用数学公式,对从 $$0\sim2^n-1$$的所有整数,转化为格雷码。
4444

45-
方法2,`n`比特的格雷码,可以递归地从`n-1`比特的格雷码生成。如图\S \ref{fig:gray-code-construction}所示。
45+
方法2,`n`比特的格雷码,可以递归地从`n-1`比特的格雷码生成。如下图所示。
4646

47-
![Gray code](../../images/gray-code-construction.png "The first few steps of the reflect-and-prefix method.")
47+
![The first few steps of the reflect-and-prefix method.](../../images/gray-code-construction.png)
4848

4949

5050
### 数学公式

0 commit comments

Comments
 (0)