File tree Expand file tree Collapse file tree 3 files changed +212
-117
lines changed Expand file tree Collapse file tree 3 files changed +212
-117
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ \subsubsection{分析}
21
21
\subsubsection {代码 }
22
22
\begin {Code }
23
23
// LeetCode, Valid Parentheses
24
+ // 时间复杂度O(n),空间复杂度O(n)
24
25
class Solution {
25
26
public:
26
27
bool isValid (string const& s) {
@@ -70,7 +71,7 @@ \subsubsection{分析}
70
71
\subsubsection {代码 }
71
72
\begin {Code }
72
73
// LeetCode, Longest Valid Parenthese
73
- // 使用栈,复杂度 O (n)
74
+ // 使用栈,时间复杂度O(n),空间复杂度O (n)
74
75
class Solution {
75
76
public:
76
77
int longestValidParentheses(string s) {
@@ -141,21 +142,21 @@ \subsubsection{分析}
141
142
\subsubsection {代码 }
142
143
\begin {Code }
143
144
// LeetCode, Largest Rectangle in Histogram
145
+ // 时间复杂度O(n),空间复杂度O(n)
144
146
class Solution {
145
147
public:
146
148
int largestRectangleArea(vector<int> &height) {
147
149
stack<int> s;
148
150
height.push_back(0);
149
151
int result = 0;
150
- for (int i = 0; i < height.size(); i++ ) {
152
+ for (int i = 0; i < height.size(); ) {
151
153
if (s.empty() || height[i] > height[s.top()])
152
- s.push(i);
154
+ s.push(i++ );
153
155
else {
154
156
int tmp = s.top();
155
157
s.pop();
156
158
result = max(result,
157
159
height[tmp] * (s.empty() ? i : i - s.top() - 1));
158
- i--;
159
160
}
160
161
}
161
162
return result;
You can’t perform that action at this time.
0 commit comments