Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 87a5f6f

Browse files
zhangzz2015gitbook-bot
authored andcommitted
GitBook: [greyireland#94] No subject
1 parent bc2ab03 commit 87a5f6f

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

‎SUMMARY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
## 数据结构篇
1111

1212
* [栈和队列](data\_structure/stack\_queue.md)
13+
* [单调栈](shu-ju-jie-gou-pian/dan-tiao-zhan.md)
1314
* [链表](data\_structure/linked\_list.md)
1415
* [二叉树和递归](data\_structure/binary\_tree.md)
1516
* [二进制](data\_structure/binary\_op.md)

‎data_structure/binary_op.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ public:
330330

331331
```
332332
333+
####
334+
333335
## 练习
334336
335337
* [ ] [single-number](https://leetcode-cn.com/problems/single-number/)

‎shu-ju-jie-gou-pian/dan-tiao-zhan.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 单调栈
2+
3+
单调栈用来查找左边或者右边第一个大/小的元素
4+
5+
```cpp
6+
// Some code
7+
// calculate leftSmall and right small index. If it is -1, mean it hasnot.
8+
void findNextSmall(vector<int>& num)
9+
{
10+
int n = nums.size();
11+
vector<int> leftSmall(n, -1);
12+
vector<int> rightSmall(n, -1);
13+
vector<int> stack;
14+
for(int i=0; i< num.size(); i++)
15+
{
16+
while(stack.size() && num[stack.back()]>num[i])
17+
{
18+
int topIndex = stack.back();
19+
rightSmall[topInex] = i;
20+
stack.pop_back();
21+
if(stack.size())
22+
leftSmall[topIndex] = stack.back();
23+
}
24+
stack.push_back(i);
25+
}
26+
for(int i=stack.size()-1; i>0; i--)
27+
{
28+
leftSmall[i] = stack[i-1];
29+
}
30+
}
31+
32+
// Some code
33+
// calculate leftSmall and right small index. If it is -1, mean it hasnot.
34+
void findNextLarge(vector<int>& num)
35+
{
36+
int n = nums.size();
37+
vector<int> leftLarge(n, -1);
38+
vector<int> rightLarge(n, -1);
39+
vector<int> stack;
40+
for(int i=0; i< num.size(); i++)
41+
{
42+
while(stack.size() && num[stack.back()]<num[i])
43+
{
44+
int topIndex = stack.back();
45+
rightLarge[topInex] = i;
46+
stack.pop_back();
47+
if(stack.size())
48+
leftLarge[topIndex] = stack.back();
49+
}
50+
stack.push_back(i);
51+
}
52+
for(int i=stack.size()-1; i>0; i--)
53+
{
54+
leftLarge[i] = stack[i-1];
55+
}
56+
}
57+
58+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /