-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode.cpp
More file actions
698 lines (633 loc) · 21.1 KB
/
Copy pathLeetCode.cpp
File metadata and controls
698 lines (633 loc) · 21.1 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include "LeetCode.h"
#include <dbg.h>
#include <cassert>
#include <array>
#include <stack>
#include <fmt/format.h>
#include <folly/Range.h>
#include <unordered_map>
#include <TreeAlgo.h>
using std::string;
void
LeetCode::fib() {
constexpr auto answer = [] {
constexpr int n = 3;
constexpr auto fib_impl = [] {
std::array<int, 31> fib{0, 1};
for (int i = 2; i != 31; ++i) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib;
}();
return fib_impl[n];
};
}
void
LeetCode::tribonacci() {
//T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
constexpr auto fib_impl = [] {
std::array<int, 37> fib{0, 1, 1};
for (int i = 3; i != 31; ++i) {
fib[i] = fib[i - 1] + fib[i - 2] + fib[i - 3];
}
return fib;
}();
auto answer = [&](int n) {
return fib_impl[n];
};
}
void
LeetCode::minCostClimbingStairs() {
std::vector<int> data = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
const auto minCostClimbingStairsImpl = [](vector<int> &cost) {
std::unordered_map<int, int> cache;
const auto impl = [&](int cur, int spend, auto &&self) -> int {
if (cur == cost.size()) {
return spend;
}
if (cur > cost.size()) {
return INT_MAX;
}
int msg = cur * 1000 + spend;
if (cache.find(msg) != cache.end()) {
return cache[msg];
}
cache[msg] = std::min(self(cur + 2, spend + cost[cur], self),
self(cur + 1, spend + cost[cur], self));
return cache[msg];
};
};
const auto dpAnswer = [](vector<int> &cost) {
// 将不同的元素的对应起来
// dp[i]=min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2])
// dp[i] 为到达第i级阶梯的所需要的最小代价 接着向上走则需要花费当前的cost[i]
// 同时 有两种方法到达 相关的 从i-1 或者i-2位置向上走
// dp[i] = min(dp[i-1]+cost[i-1] ,dp[i-2]+cost[i-2])
int first = 0;
int second = 0; //dp[0] dp[1]
for (int i = 2; i <= cost.size(); i++) //最后到达 size()处 需要多少
{
//到达 i 位置则
int tmp = std::min(first + cost[i - 2], second + cost[i - 1]);
first = second;
second = tmp;
}
return second;
};
}
void
LeetCode::findAnagrams() {
//给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。
// 异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。
/*
* 输入: s = "cbaebabacd", p = "abc"
输出: [0,6]
*/
//auto findAnagramsEnum = [](string s, string p)->vector<int> {
// // 直接暴力匹配当前出现的字符串
// string::size_type sSize = s.size();
// string::size_type pSize = p.size();
// if (sSize < pSize)return {};
// std::array<int, 26> count_p = { 0 };
// for (char pChar : p) {
// count_p[static_cast<int>(pChar)] += 1;
// }
// std::array<int, 26> check_count = { 0 };
// int first = 0;
// int second = 1;
// check_count[static_cast<int>(s[0])] += 1;
// string::size_type lastFirst = sSize - pSize;
//
// auto compare = [&]()->std::pair<int, int> {
// for (int i = 0; i < 26; i++)
// {
// if (count_p[i] == check_count[i]) {
// continue;
// }
// else if (count_p[i] > check_count[i]) {
// return { -1,i };
// }
// else {
// return { 1, i };
// }
// }
// return { 0,0 };
// };
// vector<int> result;
// while (first <= lastFirst and second < s.size())
// {
// auto compareResult = compare();
// if (compareResult.first == 0) {
// result.push_back(first);
// check_count[s[first]] -= 1;
// first++;
// }
// else if (compareResult.first > 0) {
// check_count[s[first]] -= 1;
// first++;
// if (first <= lastFirst) {
// check_count[s[first]] += 1;
// }
// }
// else {
// if(s[second] ==)
// }
// }
///std::equal(count_p.begin(), count_p.end(), check_count.begin(), check_count.end());
//};
string first = "abacbabc";
string second = "abc";
auto findAnagramsEnum = [](string s, string p) -> vector<int> {
string::size_type sSize = s.size();
string::size_type pSize = p.size();
if (sSize < pSize)
return {};
std::array<int, 26> firstCount{};
std::array<int, 26> secondCount{};
for (int i = 0; i < pSize; i++) {
firstCount[static_cast<int>(p[i] - 'a')]++;
secondCount[static_cast<int>(s[i] - 'a')]++;
}
vector<int> result;
if (std::equal(firstCount.begin(), firstCount.end(), secondCount.begin(), secondCount.end())) {
dbg(firstCount, secondCount);
result.push_back(0);
}
for (int i = pSize; i < sSize; i++) {
// 将当前的 pSize 添加进入
secondCount[static_cast<int>(s[i - pSize] - 'a')]--;
secondCount[static_cast<int>(s[i] - 'a')]++;
dbg(secondCount);
if (std::equal(firstCount.begin(), firstCount.end(), secondCount.begin(), secondCount.end())) {
result.push_back(i - pSize + 1);
}
}
return result;
};
//dbg(findAnagramsEnum(first, second));
const auto answerslidingWindow = [](string const &s, string const &p) -> vector<int> {
// 将当前存在的 range 标识为 滑动窗口的 range
string::size_type sSize = s.size();
string::size_type pSize = p.size();
if (sSize < pSize)
return {};
std::array<int, 26> firstCount{};
std::array<int, 26> secondCount{};
for (int i = 0; i < pSize; i++) {
firstCount[static_cast<int>(p[i] - 'a')]++;
}
int left = 0;
vector<int> result;
for (int right = 0; right < sSize; right++) {
// 将其填入当前所匹配的子串中
int curGet = static_cast<int>(s[right] - 'a');
secondCount[curGet]++;
auto range = folly::Range(s.begin() + left, s.begin() + right + 1);
auto cur = fmt::format(" left {} right {} cur {}", left, right, s[left]);
dbg(cur, range);
while (secondCount[curGet] > firstCount[curGet]) {
//当前的填入的字符 超过了当前的总数
// left 一直回退 直到当前情况下的 secondCount[curGet] ==firstCount[curGet]
// 最坏情况是 left 到达了 right 位置
auto range = folly::Range(s.begin() + left + 1, s.begin() + right + 1);
int curLeft = static_cast<int>(s[left] - 'a');
secondCount[curLeft]--;
left++;
string cur = fmt::format(" left-1 {} right {} cur {}", left - 1, right, left);
dbg(range, cur);
}
if (right - left + 1 == pSize) {
result.push_back(left);
dbg("result.push_back(left) " + std::to_string(left));
}
}
return result;
};
dbg(answerslidingWindow("cbaebabacd", "abc"));
}
void
LeetCode::numSubarrayProductLessThanK() {
// 双指针 将当前的窗口缩小到合适位置
const auto answerDp = [](vector<int> &nums, int k) -> int {
int cur = 1;
int left = 0;
int result = 0;
vector<int>::size_type numSize = nums.size();
for (int right = 0; right < numSize; right++) {
cur = cur * nums[right];
if (cur < k) {
if (right > left + 1) { //包含了多个元素的成绩证明可以添加多个进入
result += right - left + 1;
const auto &range = folly::Range(
nums.begin() + left, nums.begin() + right + 1);
dbg(range, left, right);
} else {
result++;
}
}
while (cur > k) {
// 左侧指针前进到
cur /= nums[left];
left++;
}
}
return result;
};
std::vector<int> data = {10, 5, 2, 6};
answerDp(data, 100);
}
void
LeetCode::allPathsSourceTarget() {
auto answer = [](vector<vector<int>> &graph) -> vector<vector<int>> {
size_t graphSize = graph.size();
std::vector<std::vector<int>> result;
std::vector<int> curResult;
auto dfs = [&](int curPos, auto &&dfs) -> void {
curResult.push_back(curPos);
if (curPos == graphSize - 1) {
result.push_back(curResult);
} else {
for (int nextPos: graph[curPos]) {
dfs(nextPos, dfs);
result.pop_back();
}
}
};
dfs(0, dfs);
return result;
};
}
void
LeetCode::findTargetSumWays() {
auto answer_ = [](vector<int> &nums, int target) {
int result = 0;
// dfs
std::vector<int> data{1, 1, 1, 1, 1};
// 递归lambda表达式
auto dfs = [&](int index, int cur, auto dfs) {
if (index == data.size()) {
if (cur == target) {
result++;
}
return;
}
dfs(index + 1, cur + data[index], dfs);
dfs(index + 1, cur - data[index], dfs);
};
// 动态规划
auto dynamic = [&]() {
int sum = 0;
for (int num: nums)
sum += num;
if (pow(sum, 2) < pow(target, 2) || (sum + target) % 2 == 1)
return 0;
target = (sum + target) / 2;
vector<int> dp(target + 1);
dp[0] = 1;
for (int num: nums) {
for (int i = target; i >= num; i--) {
dp[i] = dp[i] + dp[i - num];
}
}
return dp[target];
};
};
}
void
LeetCode::remove_all_words() {
vector<string> words = {"owuxnmzhus", "umhowszxun"};
// 删除对应字母的 异位词
vector<long> con;
con.reserve(words.size());
constexpr int cur[26] = {
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
89,
97,
101,
103,
107,
};
for (auto begin = words.begin(); begin != words.end(); ++begin) {
double count = 1;
for (char i: *begin) {
count *= cur[i - 'a'];
}
con.emplace_back(count);
}
for (int i = 1; i < words.size(); ++i) {
while (i - 1 >= 0 and i < con.size() and con[i] == con[i - 1]) {
//触发删除操作
con.erase(con.begin() + i);
words.erase(words.begin() + i);
dbg(i, words, con);
}
}
}
int
LeetCode::maxConsecutive(int bottom, int top, vector<int> &special) {
std::sort(special.begin(), special.end());
int count = 0;
int cur = 0;
auto size = special.size();
int max = 0;
for (int i = bottom; i <= top; ++i) {
if (cur < size and i == special[cur]) {
++cur;
if (count > max) {
max = count;
}
count = 0;
} else {
// 前进n 步 首先当前就不等于
if (cur < size) {
count = special[cur] - i;
i = special[cur] - 1;
} else {
count = top - i + 1;
i = top;
}
}
if (count > max) {
max = count;
}
}
return max;
}
int
LeetCode::largestCombination() {
vector<int> candidates{16, 17};
auto size = candidates.size();
if (size == 1)
return candidates[0];
int maxElem = *std::max_element(candidates.begin(), candidates.end());
double forwardBits = std::log2(maxElem) + 1;
int max = 1;
for (int i = 0; i < forwardBits; ++i) {
int cur = 1 << i;
int count = 0;
for (int a: candidates) {
if ((a & cur) != 0)
++count;
}
// 压缩空间
auto answer_depression = [&]() {
// [0 ,mapSize ] 代表map中的值
int mapSize = map.size();
dbg(mapSize);
std::vector<int> val(mapSize, 1); //只使用一行作为当前的存储
for (int row = 0; row < target; ++row) {
auto iter = map.begin();
auto mapEnd = map.end();
for (int col = 0; col < mapSize; ++col, ++iter) {
int maxElem = iter->first * iter->second;
// 将相同值的部分叠加起来 注意需要叠加的值 不能越界
dbg(maxElem);
if (maxElem == 0) {
val[col] = val[col] * iter->second;
continue;
}
for (int i = -maxElem; i <= maxElem; i += iter->first * 2) {
dbg(i, col);
if (col - i >= 0 and col - i < mapSize) {
dbg(col - i);
val[col] += val[col - i];
}
void
LeetCode::digitSum() {
string data = "11111222223";
int k = 3;
while (data.size() > k) {
for (int i = 0; i < data.size();) {
dbg(data);
int cur = 0;
for (int j = 0; j < k and i + j < data.size(); ++j) {
cur += data[i + j] - '0';
}
int count = std::min(static_cast<int>((data.size() - i)), k);
dbg(i, count);
const string &Right = std::to_string(cur);
data.replace(i, i + count, Right);
i += Right.size();
data.erase(i, count - k);// 将实验上的部分给结束
i += k;
}
}
dbg(data);
}
void
LeetCode::intersection() {
// vector<vector<int>>& nums
vector<vector<int>> nums;
std::unordered_set<int> cur;
std::unordered_set<int> other;
for (int &i: nums[0]) {
cur.insert(i);
}
for (int i = 1; i < nums.size(); ++i) {
for (int v: nums[i]) {
if (cur.find(v) != cur.end()) {
other.insert(v);
}
}
cur = std::move(other);
other.clear();
}
vector<int> result(cur.begin(), cur.end());
}
void
LeetCode::countLatticePoints() {
// 遍历所有节点
//vector<vector<int>>& circles
vector<vector<int>> circles = {{2, 2, 2},
{3, 4, 1}};
int result = 0;
if (circles.size() == 1) {
result = 5;
return;
}
// 一个 set fixme 错位枚举法
{
struct PairHash {
size_t
operator()(const std::pair<int, int> &p) const {
return std::hash<int>()(p.first) ^ std::hash<int>()(p.second);
}
};
std::unordered_set<std::pair<int, int>, PairHash> set;
// 1/4 的数量
const auto one_circle = [&set](const vector<int> &circle) {
std::pair<int, int> center{circle[0], circle[1]};
int radius = circle[2];
// 添加五个点进入数组中去
// set.reserve(set.size()+5);
set.insert(center);
set.insert({center.first - radius, center.second});
set.insert({center.first + radius, center.second});
set.insert({center.first, center.second - radius});
set.insert({center.first, center.second + radius});
// 判断最大的正方体的区域的点
std::pair<int, int> rightTop{center.first + radius - 1, center.second + radius - 1};
while (rightTop.first != center.first and rightTop.second != center.second) {
double distance = std::sqrt(
std::pow(rightTop.first - center.first, 2) +
std::pow(rightTop.second - center.second, 2));
if (distance < radius) {
// 确定又上顶点 将顶点加入到set 中去
int startX = rightTop.first - (rightTop.first - center.first) * 2;
int endY = rightTop.second - (rightTop.second - center.second) * 2;
// dbg(startX, endY)
for (int x = startX; x <= rightTop.first; ++x) {
for (int y = rightTop.second; y >= endY; --y) {
set.insert({x, y});
}
}
break;
}
--rightTop.first;
--rightTop.second;
}
};
for (const vector<int> &circle: circles) {
one_circle(circle);
}
dbg(set);
string dbg_str;
for (auto i: set) {
dbg_str.append("[" + std::to_string(i.first) + "," + std::to_string(i.second) + "]");
}
dbg(dbg_str);
}
// return set.size();
// 差分方法
int mx = circles[0][0];
int my = circles[0][1];
for (const auto &v: circles) {
if (v[0] > mx)
mx = v[0];
if (v[1] > my)
my = v[1];
}
dbg(mx, my);
vector<vector<int>> diff(mx + 2, vector<int>(my + 1));
for (const vector<int> &data: circles) {
int curX = data[0];
int curY = data[1];
int radius = data[2];
for (int y = curY - radius; y <= curY + radius; ++y) {
int z = floor(sqrt(radius * radius - (y - curY) * (y - curY)));
dbg(z);
diff[curX - z][y] += 1;
diff[curX + z + 1][curY] -= 1;
}
dbg(diff);
}
result = std::accumulate(diff.begin(),
diff.end(),
0,
[](int a, const vector<int> &val) {
return a + std::accumulate(val.begin(),
val.end(),
0,
[](int c, int a) { return c + (a > 0 ? 1 : 0); });
});
}
void LeetCode::flattenList() {
class Node {
public:
int val;
Node *prev;
Node *next;
Node *child;
};
auto root = Node{};
auto head = std::addressof(root);
if (head == nullptr)return;
std::stack<Node *, std::vector<Node *>> mStack;
while (head->next != nullptr or not mStack.empty()) {
// 到达了当前的层的最后一个节点
if (head->next == nullptr) {
if (head->child != nullptr) {
// 直接将当前节点转向一个下一个节点
head->next = head->child;
head = head->next;
} else {
// 寻找当前的栈中的所存在的元素
while (not mStack.empty()) {
Node *i = mStack.top();
mStack.pop();
// 连线当前节点的数据
head->next = i->next;
while (head->next != nullptr) {
head = head->next;
}
}
}
} else {
// 当前的数据 需要考虑是否为一个 直接悬挂的链表节点
if (head->child != nullptr) {
mStack.push(head);
head = head->child;
} else {
head = head->next;
}
}
}
}
void LeetCode::largestGoodInteger() {
string const &num = "6777133339";
int result = INT_MIN;
int size = num.size();
for (int i = 0; i < size - 3; i += 3) {
if (num[i] == num[i + 1] && num[i + 1] == num[i + 2]) {
int cur = std::stoi(num.substr(i, 3));
if (cur > result) {
result = cur;
}
}
}
// return result == INT_MIN ? "" : result == 0 ? "000" : std::to_string(result);
}
void LeetCode::averageOfSubtree() {
struct CountResult {
int value = 0;
int node_num = 0;
void add(const CountResult &rhs) {
this->value += rhs.value;
this->node_num += rhs.node_num;
}
};
int answer = 0;
auto count = [&answer](TreeNode *root, auto count) { // 返回值和节点总数
if (root == nullptr)return CountResult{};
CountResult result{root->value, 1};
result.add(count(root->left, count));
result.add(count(root->right, count));
if (result.value / result.node_num == root->value) {
++answer;
}
return result;
};
// const std::unique_ptr<TreeNode> &ptr = TreeAlgo::create_tree();
}
void LeetCode::countTexts() {
}