Skip to content

Commit 630c8d0

Browse files
committed
添加时间复杂度和空间复杂度
1 parent a975bc1 commit 630c8d0

File tree

6 files changed

+49
-19
lines changed

6 files changed

+49
-19
lines changed

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

7.96 KB
Binary file not shown.

C++/chapBFS.tex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ \subsubsection{分析}
4141
\subsubsection{代码}
4242
\begin{Code}
4343
//LeetCode, Word Ladder
44+
// 时间复杂度O(n),空间复杂度O(n)
4445
class Solution {
4546
public:
4647
typedef string state_t;
@@ -141,6 +142,7 @@ \subsubsection{代码}
141142

142143
\begin{Code}
143144
//LeetCode, Word Ladder II
145+
// 时间复杂度O(n),空间复杂度O(n)
144146
class Solution {
145147
public:
146148
vector<vector<string> > findLadders(string start, string end,
@@ -250,6 +252,7 @@ \subsubsection{代码}
250252
\begin{Code}
251253
// LeetCode, Surrounded Regions
252254
// BFS
255+
// 时间复杂度O(n),空间复杂度O(n)
253256
class Solution {
254257
public:
255258
void solve(vector<vector<char>> &board) {
@@ -278,8 +281,8 @@ \subsubsection{代码}
278281
visit(board, i, j, q);
279282
while (!q.empty()) {
280283
int cur = q.front(); q.pop();
281-
const int x = cur / board[0].size();
282-
const int y = cur % board[0].size();
284+
int x = cur / board[0].size();
285+
int y = cur % board[0].size();
283286
visit(board, x - 1, y, q);
284287
visit(board, x, y - 1, q);
285288
visit(board, x + 1, y, q);

C++/chapDivideAndConquer.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ \subsubsection{代码}
1717
\begin{Code}
1818
//LeetCode, Pow(x, n)
1919
// 二分法,$x^n = x^{n/2} * x^{n/2} * x^{n\%2}$
20+
// 时间复杂度O(logn),空间复杂度O(1)
2021
class Solution {
2122
public:
2223
double pow(double x, int n) {
@@ -58,6 +59,7 @@ \subsubsection{代码}
5859
\begin{Code}
5960
// LeetCode, Longest Substring Without Repeating Characters
6061
// 二分查找
62+
// 时间复杂度O(logn),空间复杂度O(1)
6163
class Solution {
6264
public:
6365
int sqrt(int x) {

C++/chapGraph.tex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ \subsubsection{描述}
2222
OJ's undirected graph serialization:
2323
Nodes are labeled uniquely.
2424

25-
We use \code{#} as a separator for each node, and \code{,} as a separator for node label and each neighbour of the node.
26-
As an example, consider the serialized graph \code{\{0,1,2#1,2#2,2\}}.
25+
We use \code{\#} as a separator for each node, and \code{,} as a separator for node label and each neighbour of the node.
26+
As an example, consider the serialized graph \code{\{0,1,2\#1,2\#2,2\}}.
2727

28-
The graph has a total of three nodes, and therefore contains three parts as separated by \code{#}.
28+
The graph has a total of three nodes, and therefore contains three parts as separated by \code{\#}.
2929
\begin{enumerate}
3030
\item First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
3131
\item Second node is labeled as 1. Connect node 1 to node 2.
@@ -50,7 +50,7 @@ \subsubsection{分析}
5050
\subsubsection{代码}
5151
\begin{Code}
5252
// LeetCode, Clone Graph
53-
// DFS
53+
// DFS,时间复杂度O(n),空间复杂度O(n)
5454
class Solution {
5555
public:
5656
UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node) {
@@ -80,7 +80,7 @@ \subsubsection{代码}
8080

8181
\begin{Code}
8282
// LeetCode, Clone Graph
83-
// BFS
83+
// BFS,时间复杂度O(n),空间复杂度O(n)
8484
class Solution {
8585
public:
8686
UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node) {

C++/chapGreedy.tex

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,21 @@ \subsubsection{分析}
3434
\subsubsection{代码}
3535
\begin{Code}
3636
// LeetCode, Jump Game
37-
// 思路一
37+
// 思路1,时间复杂度O(n),空间复杂度O(1)
3838
class Solution {
3939
public:
4040
bool canJump(int A[], int n) {
41-
int right_most = 0; // 最右能跳到哪里
42-
for (int start = 0; start <= right_most; start++) {
43-
if (A[start] + start > right_most)
44-
right_most = A[start] + start;
45-
46-
if (right_most >= n - 1) return true;
47-
}
48-
return false;
41+
int reach = 1; // 最右能跳到哪里
42+
for (int i = 0; i < reach && reach < n; ++i)
43+
reach = max(reach, i + 1 + A[i]);
44+
return reach >= n;
4945
}
5046
};
5147
\end{Code}
5248

5349
\begin{Code}
5450
// LeetCode, Jump Game
55-
// 思路二
51+
// 思路2,时间复杂度O(n),空间复杂度O(1)
5652
class Solution {
5753
public:
5854
bool canJump (int A[], int n) {
@@ -71,7 +67,7 @@ \subsubsection{代码}
7167

7268
\begin{Code}
7369
// LeetCode, Jump Game
74-
// 思路三,动规
70+
// 思路三,动规,时间复杂度O(n),空间复杂度O(n)
7571
class Solution {
7672
public:
7773
bool canJump(int A[], int n) {
@@ -117,6 +113,7 @@ \subsubsection{分析}
117113
\subsubsection{代码}
118114
\begin{Code}
119115
// LeetCode, Jump Game II
116+
// 时间复杂度O(n),空间复杂度O(1)
120117
class Solution {
121118
public:
122119
int jump(int A[], int n) {
@@ -141,6 +138,30 @@ \subsubsection{代码}
141138
};
142139
\end{Code}
143140

141+
\begin{Code}
142+
// LeetCode, Jump Game II
143+
// 时间复杂度O(n),空间复杂度O(1)
144+
class Solution {
145+
public:
146+
int jump(int A[], int n) {
147+
int result = 0;
148+
// the maximum distance that has been reached
149+
int last = 0;
150+
// the maximum distance that can be reached by using "ret+1" steps
151+
int cur = 0;
152+
for (int i = 0; i < n; ++i) {
153+
if (i > last) {
154+
last = cur;
155+
++result;
156+
}
157+
cur = max(cur, i + A[i]);
158+
}
159+
160+
return result;
161+
}
162+
};
163+
\end{Code}
164+
144165

145166
\subsubsection{相关题目}
146167
\begindot
@@ -166,6 +187,7 @@ \subsubsection{分析}
166187
\subsubsection{代码}
167188
\begin{Code}
168189
// LeetCode, Best Time to Buy and Sell Stock
190+
// 时间复杂度O(n),空间复杂度O(1)
169191
class Solution {
170192
public:
171193
int maxProfit(vector<int> &prices) {
@@ -208,6 +230,7 @@ \subsubsection{分析}
208230
\subsubsection{代码}
209231
\begin{Code}
210232
// LeetCode, Best Time to Buy and Sell Stock II
233+
// 时间复杂度O(n),空间复杂度O(1)
211234
class Solution {
212235
public:
213236
int maxProfit(vector<int> &prices) {
@@ -251,7 +274,7 @@ \subsubsection{分析}
251274
\subsubsection{代码}
252275
\begin{Code}
253276
// LeetCode, Longest Substring Without Repeating Characters
254-
// 贪心法
277+
// 时间复杂度O(n),空间复杂度O(1)
255278
class Solution {
256279
public:
257280
int lengthOfLongestSubstring(string s) {
@@ -297,6 +320,7 @@ \subsubsection{分析}
297320
\subsubsection{代码}
298321
\begin{Code}
299322
// LeetCode, Container With Most Water
323+
// 时间复杂度O(n),空间复杂度O(1)
300324
class Solution {
301325
public:
302326
int maxArea(vector<int> &height) {

C++/leetcode-cpp.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
\include{chapDivideAndConquer}
4949
\include{chapGreedy}
5050
\include{chapDynamicProgramming}
51+
\include{chapGraph}
5152
\include{chapImplement}
5253

5354
\appendix % 开始附录,章用字母编号

0 commit comments

Comments
 (0)