@@ -1224,60 +1224,60 @@ vector<vector<string>> groupAnagrams(vector<string>& strs)
1224
1224
- 枚举数组中的每个数$x$作为起点,不断尝试匹配$x+n$是否存在,假设最长匹配到了$x+y$,那么以$x$为起点的最长连续序列长度即为$y+1$,不断枚举并更新答案即可
1225
1225
1226
1226
```cpp
1227
- // bool findNum(vector<int>& nums, int target)
1228
- // {
1229
- // for (int num : nums)
1230
- // {
1231
- // if (num == target)
1232
- // return true;
1233
- // }
1234
- // return false;
1235
- // }
1227
+ bool findNum(vector<int>& nums, int target)
1228
+ {
1229
+ for (int num : nums)
1230
+ {
1231
+ if (num == target)
1232
+ return true;
1233
+ }
1234
+ return false;
1235
+ }
1236
1236
1237
- // int longestConsecutive(vector<int>& nums)
1238
- // {
1239
- // int maxSeq = 0;
1240
- // for (int num : nums)
1241
- // {
1242
- // int it = 1;
1243
- // int accum = 1;
1244
- // while (findNum(nums, num + it))
1245
- // {
1246
- // accum++;
1247
- // it++;
1248
- // }
1249
- // if (accum > maxSeq)
1250
- // maxSeq = accum;
1251
- // }
1252
- // return maxSeq;
1253
- // }
1237
+ int longestConsecutive(vector<int>& nums)
1238
+ {
1239
+ int maxSeq = 0;
1240
+ for (int num : nums)
1241
+ {
1242
+ int it = 1;
1243
+ int accum = 1;
1244
+ while (findNum(nums, num + it))
1245
+ {
1246
+ accum++;
1247
+ it++;
1248
+ }
1249
+ if (accum > maxSeq)
1250
+ maxSeq = accum;
1251
+ }
1252
+ return maxSeq;
1253
+ }
1254
1254
```
1255
1255
1256
1256
- 我们只需首先遍历一次将数放到哈希集合中,就可以优化每次搜索的复杂度了,但此时仍会存在极端测试用例卡超时
1257
1257
1258
1258
``` cpp
1259
- // int longestConsecutive(vector<int>& nums)
1260
- // {
1261
- // unordered_set<int> ht;
1262
- // for(int num : nums)
1263
- // ht.insert(num);
1259
+ int longestConsecutive (vector<int >& nums)
1260
+ {
1261
+ unordered_set<int > ht;
1262
+ for(int num : nums)
1263
+ ht.insert(num);
1264
1264
1265
- // int maxSeq = 0;
1265
+ int maxSeq = 0;
1266
1266
1267
- // for (int num : nums)
1268
- // {
1269
- // int accum = 1;
1270
- // int it = 1;
1271
- // while (ht.count(num + it))
1272
- // {
1273
- // accum++;
1274
- // it++;
1275
- // }
1276
- // if (accum > maxSeq)
1277
- // maxSeq = accum;
1278
- // }
1279
- // return maxSeq;
1280
- // }
1267
+ for (int num : nums)
1268
+ {
1269
+ int accum = 1;
1270
+ int it = 1;
1271
+ while (ht.count(num + it))
1272
+ {
1273
+ accum++;
1274
+ it++;
1275
+ }
1276
+ if (accum > maxSeq)
1277
+ maxSeq = accum;
1278
+ }
1279
+ return maxSeq;
1280
+ }
1281
1281
```
1282
1282
1283
1283
- 我们思考继续优化,我们需要遍历每个元素查找其向上的最大连续长度,当我们遍历到每个元素的时候,可以先检测其加上当前的最大长度所得结果值是否存在于数组中,若无则跳过该数,这就省去了对那些对最大长度不可能有贡献的元素的迭代
@@ -1286,67 +1286,67 @@ vector<vector<string>> groupAnagrams(vector<string>& strs)
1286
1286
1287
1287
```cpp
1288
1288
//第一个解法
1289
- // int longestConsecutive(vector<int>& nums)
1290
- // {
1291
- // if (nums.size() == 0)
1292
- // return 0;
1293
- //
1294
- // unordered_set<int> ht;
1295
- // for(int num : nums)
1296
- // ht.insert(num);
1297
- //
1298
- // int maxSeq = 1;
1299
- //
1300
- // for (int num : nums)
1301
- // {
1302
- // // int accum = 1;
1303
- // int accum = maxSeq;
1304
- //
1305
- // // int it = 1;
1306
- // int it = maxSeq;
1307
- //
1308
- // while (ht.count(num + it))
1309
- // {
1310
- // accum++;
1311
- // it++;
1312
- // }
1313
- // if (accum > maxSeq)
1314
- // maxSeq = accum;
1315
- // }
1316
- // return maxSeq;
1317
- // }
1289
+ int longestConsecutive(vector<int>& nums)
1290
+ {
1291
+ if (nums.size() == 0)
1292
+ return 0;
1293
+
1294
+ unordered_set<int> ht;
1295
+ for(int num : nums)
1296
+ ht.insert(num);
1297
+
1298
+ int maxSeq = 1;
1299
+
1300
+ for (int num : nums)
1301
+ {
1302
+ // int accum = 1;
1303
+ int accum = maxSeq;
1304
+
1305
+ // int it = 1;
1306
+ int it = maxSeq;
1307
+
1308
+ while (ht.count(num + it))
1309
+ {
1310
+ accum++;
1311
+ it++;
1312
+ }
1313
+ if (accum > maxSeq)
1314
+ maxSeq = accum;
1315
+ }
1316
+ return maxSeq;
1317
+ }
1318
1318
1319
1319
//第二个解法
1320
- // int longestConsecutive(vector<int>& nums)
1321
- // {
1322
- // if (nums.size() == 0)
1323
- // return 0;
1324
- //
1325
- // unordered_set<int> ht;
1326
- // for(int num : nums)
1327
- // ht.insert(num);
1328
- //
1329
- // int maxSeq = 1;
1330
- //
1331
- // for (int num : nums)
1332
- // {
1333
- // int accum = 1;
1334
- // int it = 1;
1335
- //
1336
- // //检测当前元素num是否具有更新maxSeq的潜力
1337
- // if (ht.count(num + maxSeq))
1338
- // {
1339
- // while (ht.count(num + it))
1340
- // {
1341
- // accum++;
1342
- // it++;
1343
- // }
1344
- // if (accum > maxSeq)
1345
- // maxSeq = accum;
1346
- // }
1347
- // }
1348
- // return maxSeq;
1349
- // }
1320
+ int longestConsecutive(vector<int>& nums)
1321
+ {
1322
+ if (nums.size() == 0)
1323
+ return 0;
1324
+
1325
+ unordered_set<int> ht;
1326
+ for(int num : nums)
1327
+ ht.insert(num);
1328
+
1329
+ int maxSeq = 1;
1330
+
1331
+ for (int num : nums)
1332
+ {
1333
+ int accum = 1;
1334
+ int it = 1;
1335
+
1336
+ //检测当前元素num是否具有更新maxSeq的潜力
1337
+ if (ht.count(num + maxSeq))
1338
+ {
1339
+ while (ht.count(num + it))
1340
+ {
1341
+ accum++;
1342
+ it++;
1343
+ }
1344
+ if (accum > maxSeq)
1345
+ maxSeq = accum;
1346
+ }
1347
+ }
1348
+ return maxSeq;
1349
+ }
1350
1350
```
1351
1351
1352
1352
- 但即便如此还是会超时,我们再次思考优化方法,我们其实可以对有前缀元素的元素直接跳过,每次只从所有连续元素的最开头的元素开始向后遍历,只需要添加一个判断条件即可
0 commit comments