-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (131 loc) · 3.55 KB
/
Copy pathmain.cpp
File metadata and controls
143 lines (131 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// main.cpp
// stack+queue
//
// Created by junl on 2019/7/18.
// Copyright © 2019 junl. All rights reserved.
//
#include <iostream>
#include "stack.h"
#include "sampleBrowser.h"
//#include "queue.h"
#include "isValid.h"
#include "longestValidParentheses.h"
#include "queueByStack.h"
#include "MinStack.h"
#include <string>
#include "TwoStacksQueue.h"
#include "reverseStack.h"
#include "sortStackByStack.h"
#include "hanoiProblem.h"
#include "getMaxWindow.h"
#include "getMaxRectSize.h"
#include "Expression.h"
#include "validateStackSequences.h"
#include <deque>
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
/*
每次滑动窗口,窗口的最大值都会更新,问题的关键在于最后最大值被划出去了,怎么样快速求出第二个最大值,因此需要保存的是最大值的列表,而不是单个值.
维护一个大顶堆的结构
*/
if (nums.size() == 0 || k<=1) return nums;
priority_queue<int, vector<int>, greater<int>> q;
vector<int> results;
for(int i=0; i < nums.size(); i++){
q.push(i);
if (i >= k-1){
if (q.top() + k <= i ){
q.pop();
}
results.push_back(nums[q.top()]);
}
}
return results;
}
};
int main(int argc, const char * argv[]) {
// insert code here...
// stack<int> stk;
// printf("stack test starting..........\n");
// stk.push(1);
// stk.push(2);
// stk.push(3);
// stk.print();
// stk.pop();
// stk.pop();
// stk.print();
// deque<int> dq;
// dq.push_back(1);
// dq.push_back(2);
// while (!dq.empty()) {
// printf("%d \n",dq.back());
// dq.pop_back();
// }
deque<int> dq;
dq.push_front(1);
dq.push_front(2);
while (!dq.empty()) {
printf("%d \n",dq.back());
dq.pop_back();
}
/*
pop_front()
2
1
pop_back()
1
2
*/
printf("sampleBrowser starting..........\n");
sampleBrowser browser;
browser.open("http://www.baidu.com");
browser.open("http://news.baidu.com/");
browser.open("http://news.baidu.com/ent");
browser.goBack();
browser.goBack();
browser.goForward();
browser.open("http://www.qq.com");
browser.goForward();
browser.goBack();
browser.goForward();
browser.checkCurrentPage();
browser.goBack();
browser.goBack();
browser.goBack();
browser.checkCurrentPage();
printf("queue_on_array starting..........\n");
//#if true
// queue_on_circular<int> queue1(5);
//#else
// queue_on_linkedlist<int> queue1;
//#endif
// queue1.enqueue(10);
// queue1.enqueue(20);
// queue1.enqueue(30);
// queue1.dequeue();
// queue1.print();
// queue1.enqueue(40);
// queue1.enqueue(50);
// queue1.print();
std::string s = "{()[]{}}";
std::cout << "有效字符串:()[]{}, " << leetcode::isValid(s) << std::endl;
s = ")()())";
std::cout << "最长有效括号: )()())" << leetcode::longestValidParentheses(s) << std::endl;
test_Minstack();
test_TwoStacksQueue();
test_reverseStack();
test_sortStackByStack();
test_hanoiProblem();
test_getMaxWindow();
test_validateStackSequences();
Expression expr;
vector<string> strs{"3", "+", "5", "-", "6", "*", "8" , "-" , "6"};
int i = expr.execut(strs);
printf("expr = [ %i]\n", i);
Solution so;
vector<int> a1{1,3,-1,-3,5,3,6,7};
so.maxSlidingWindow(a1, 3);
return 0;
}