-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode128.cpp
233 lines (222 loc) · 6.41 KB
/
leetcode128.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*************************************************
Author: wenhaofang
Date: 2023-03-12
Description: leetcode128 - Longest Consecutive Sequence
*************************************************/
#include <iostream>
#include <list>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
using std::list;
using std::vector;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::stack;
using std::queue;
using std::priority_queue;
using std::max;
using std::min;
using std::swap;
using std::pair;
using std::atoi;
using std::stoi;
using std::to_string;
using std::cin;
using std::cout;
using std::endl;
/**
* 方法一:暴力搜索
*
* 理论时间复杂度:O(n^3),其中 n 为数组大小
* 理论空间复杂度:O(1)
*/
/**
* 思路
*
* 遍历数组作为连续序列的起始元素 x,遍历数组找 x + 1、x + 2、...,直至没找到时即为一组连续序列
*/
// class Solution {
// public:
// int longestConsecutive(vector<int>& nums) {
// // 特判
// int n = nums.size();
// if (n == 0) {
// return 0;
// }
// // 暴力搜索
// int ans = 0;
// for (int i = 0; i < n; i++) { // 第 1 层循环
// int sos = nums[i];
// int eos = nums[i];
// while (1) { // 第 2 层循环
// bool find = false;
// for (int j = 0; j < n; j++) { // 第 3 层循环
// if (nums[j] == eos + 1) {
// find = true;
// eos += 1;
// break;
// }
// }
// if (!find) {
// break;
// }
// }
// ans = max(ans, eos - sos + 1);
// }
// // 返回答案
// return ans;
// }
// };
/**
* 方法二:哈希
*
* 理论时间复杂度:O(n^2),其中 n 为数组大小
* 理论空间复杂度:O(n) ,其中 n 为数组大小
*/
/**
* 思路:消解第 3 层循环
*
* 上述第 3 层循环的作用是找数组中是否存在某个数,为此,我们可以预先将数组存入哈希集合,这样时间复杂度可以降为 O(1)
*/
// class Solution {
// public:
// int longestConsecutive(vector<int>& nums) {
// // 特判
// int n = nums.size();
// if (n == 0) {
// return 0;
// }
// // 数组存入哈希集合
// unordered_set<int> set;
// for (int num:nums) {
// set.insert(num);
// }
// // 优化搜索
// int ans = 0;
// for (int i = 0; i < n; i++) { // 第 1 层循环
// int sos = nums[i];
// int eos = nums[i];
// // while (1) {
// // if (set.find(eos + 1) != set.end()) {
// // eos += 1;
// // } else {
// // break;
// // }
// // }
// while (set.find(eos + 1) != set.end()) { // 第 2 层循环
// eos += 1;
// }
// ans = max(ans, eos - sos + 1);
// }
// // 返回答案
// return ans;
// }
// };
/**
* 方法三:哈希
*
* 理论时间复杂度:O(n),其中 n 为数组大小
* 理论空间复杂度:O(n),其中 n 为数组大小
*/
/**
* 思路:优化第 2 层循环
*
* 上述第 2 层循环有很多重复计算
*
* 例如之前一步是找以 x 为起点的连续序列,此时要检查 x + 1, x + 2, ... 是否在数组
*
* 如果之后一步要找以 x - 1 为起点的连续序列,又重复检查 x, x + 1, x + 2, ... 是否在数组
*
* 所以说,如果要找以 x 为起点的连续序列,可以先检查 x - 1 是否在数组
*
* 如果在,就不用处理 ,对应的检查可以留给遍历到 x - 1 时执行,如果不在,才要去检查
*
* 实际上这样只会遍历一次数组,所以时间复杂度是 O(n)
*/
// class Solution {
// public:
// int longestConsecutive(vector<int>& nums) {
// // 特判
// int n = nums.size();
// if (n == 0) {
// return 0;
// }
// // 数组存入哈希集合
// unordered_set<int> set;
// for (int num: nums) {
// set.insert(num);
// }
// // 优化搜索
// int ans = 0;
// for (int i = 0; i < n; i++) { // 第 1 层循环
// int sos = nums[i];
// int eos = nums[i];
// if (set.find(sos - 1) != set.end()) { // 优化
// continue;
// }
// while (set.find(eos + 1) != set.end()) { // 第 2 层循环
// eos += 1;
// }
// ans = max(ans, eos - sos + 1);
// }
// // 返回答案
// return ans;
// }
// };
/**
* 方法四:
*
* 理论时间复杂度:O(n),其中 n 为数组大小
* 理论空间复杂度:O(n),其中 n 为数组大小
*/
/**
* 思路:优化第 1 层循环
*
* 上述第 1 层循环遍历的是数组,实际上遍历哈希集合就行,因为数组中的重复元素处理是一样的
*/
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
// 特判
int n = nums.size();
if (n == 0) {
return 0;
}
// 数组存入哈希集合
unordered_set<int> set;
for (int num: nums) {
set.insert(num);
}
// 优化搜索
int ans = 0;
for (int num: set) { // 第 1 层循环
int sos = num;
int eos = num;
if (set.find(sos - 1) != set.end()) { // 优化
continue;
}
while (set.find(eos + 1) != set.end()) { // 第 2 层循环
eos += 1;
}
ans = max(ans, eos - sos + 1);
}
// 返回答案
return ans;
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
vector<int> nums = {100, 4, 200, 1, 3, 2};
int ans = solution -> longestConsecutive(nums);
cout << ans << endl;
}