Skip to content

Commit f536e9e

Browse files
committed
添加了时间复杂度和空间复杂度
1 parent c4c5ea8 commit f536e9e

File tree

3 files changed

+212
-117
lines changed

3 files changed

+212
-117
lines changed

C++/LeetCodet题解(C++版).pdf

1.6 KB
Binary file not shown.

C++/chapStackAndQueue.tex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ \subsubsection{分析}
2121
\subsubsection{代码}
2222
\begin{Code}
2323
// LeetCode, Valid Parentheses
24+
// 时间复杂度O(n),空间复杂度O(n)
2425
class Solution {
2526
public:
2627
bool isValid (string const& s) {
@@ -70,7 +71,7 @@ \subsubsection{分析}
7071
\subsubsection{代码}
7172
\begin{Code}
7273
// LeetCode, Longest Valid Parenthese
73-
// 使用栈,复杂度 O(n)
74+
// 使用栈,时间复杂度O(n),空间复杂度O(n)
7475
class Solution {
7576
public:
7677
int longestValidParentheses(string s) {
@@ -141,21 +142,21 @@ \subsubsection{分析}
141142
\subsubsection{代码}
142143
\begin{Code}
143144
// LeetCode, Largest Rectangle in Histogram
145+
// 时间复杂度O(n),空间复杂度O(n)
144146
class Solution {
145147
public:
146148
int largestRectangleArea(vector<int> &height) {
147149
stack<int> s;
148150
height.push_back(0);
149151
int result = 0;
150-
for (int i = 0; i < height.size(); i++) {
152+
for (int i = 0; i < height.size(); ) {
151153
if (s.empty() || height[i] > height[s.top()])
152-
s.push(i);
154+
s.push(i++);
153155
else {
154156
int tmp = s.top();
155157
s.pop();
156158
result = max(result,
157159
height[tmp] * (s.empty() ? i : i - s.top() - 1));
158-
i--;
159160
}
160161
}
161162
return result;

0 commit comments

Comments
 (0)