mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
76
else/topic/2017.h
Normal file
76
else/topic/2017.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "../structs/Tree.h"
|
||||
#include <queue>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void reversechild(TreeNode *root)
|
||||
{
|
||||
TreeNode *head = root;
|
||||
queue<TreeNode *> q;
|
||||
q.push(head);
|
||||
while (!q.empty())
|
||||
{
|
||||
head = q.front();
|
||||
q.pop();
|
||||
if (head->left)
|
||||
{
|
||||
q.push(head->left);
|
||||
}
|
||||
if (head->right)
|
||||
{
|
||||
q.push(head->right);
|
||||
}
|
||||
swap(head->left, head->right);
|
||||
}
|
||||
}
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
unordered_map<int, int> statistic(vector<int> L1)
|
||||
{
|
||||
unordered_map<int, int> m;
|
||||
for (auto i : L1)
|
||||
{
|
||||
m[i] = m[i] + 1;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
#include "../structs/List.h"
|
||||
|
||||
ListNode *integrateList(LitNode *L1, ListNode *l2)
|
||||
{
|
||||
ListNode *head = L1->value < L2->value ? L1 : L2;
|
||||
if (L1->value < L2->value)
|
||||
{
|
||||
L1 = L1->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
L2 = L2->next;
|
||||
}
|
||||
ListNode *temp = head->next;
|
||||
while (L1 && L2)
|
||||
{
|
||||
temp->next = L1->value < L2->value ? L1 : L2;
|
||||
if (L1->value < L2->value)
|
||||
{
|
||||
L1 = L1->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
L2 = L2->next;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
if(L1){
|
||||
temp->next = L1;
|
||||
}
|
||||
if(L2){
|
||||
temp->next = L2;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
60
else/topic/2019.h
Normal file
60
else/topic/2019.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
void odd_even(vector<int> &array)
|
||||
{
|
||||
int left = 0;
|
||||
int right = array.size() - 1;
|
||||
while (left < right)
|
||||
{
|
||||
while (left < right && array[right] % 2 == 0)
|
||||
{
|
||||
right--;
|
||||
}
|
||||
while (left < right && array[left] % 2 == 1)
|
||||
{
|
||||
left++;
|
||||
}
|
||||
swap(array[left], array[right]);
|
||||
}
|
||||
}
|
||||
|
||||
#include "../structs/Tree.h"
|
||||
|
||||
void insertSearchTree(TreeNode *root, int value)
|
||||
{
|
||||
TreeNode *dist = new TreeNode();
|
||||
dist->val = value;
|
||||
if (!root)
|
||||
{
|
||||
root = dist;
|
||||
}
|
||||
TreeNode *temp = root;
|
||||
while (temp)
|
||||
{
|
||||
if (temp->val > value)
|
||||
{
|
||||
if (temp->left)
|
||||
{
|
||||
temp = temp->left;
|
||||
continue;
|
||||
}
|
||||
temp->left = dist;
|
||||
break;
|
||||
}
|
||||
if (temp->val < value)
|
||||
{
|
||||
if (temp->right)
|
||||
{
|
||||
temp = temp->right;
|
||||
continue;
|
||||
}
|
||||
temp->left = dist;
|
||||
break;
|
||||
}
|
||||
if (temp->val == value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
else/topic/2020.h
Normal file
28
else/topic/2020.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "../structs/Tree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
TreeNode *getX(TreeNode *root, int x)
|
||||
{
|
||||
TreeNode *temp = root;
|
||||
while (temp)
|
||||
{
|
||||
if (temp->val > x)
|
||||
{
|
||||
if (temp->left)
|
||||
{
|
||||
temp = temp->left;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (temp->right)
|
||||
{
|
||||
temp = temp->right;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
35
else/topic/2021.h
Normal file
35
else/topic/2021.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int test1(vector<int> array)
|
||||
{
|
||||
if (array.empty())
|
||||
{
|
||||
return INT_MIN;
|
||||
}
|
||||
stack<int> S;
|
||||
int norm = array[0];
|
||||
int m = INT_MIN;
|
||||
S.push(array[0]);
|
||||
for (int i = 1; i < array.size(); i++)
|
||||
{
|
||||
m = max(m, norm - array[i]);
|
||||
while (!S.empty() && array[i] > S.top())
|
||||
{
|
||||
S.pop();
|
||||
}
|
||||
if (S.empty())
|
||||
{
|
||||
norm = array[i];
|
||||
}
|
||||
S.push(array[i]);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
#include "../structs/Tree.h"
|
||||
int test2(TreeNode *root){
|
||||
//先镜像,再按需遍历
|
||||
}
|
||||
66
else/topic/2022.h
Normal file
66
else/topic/2022.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "../structs/List.h"
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<vector<int>> test1(ListNode *head)
|
||||
{
|
||||
vector<vector<int>> ret;
|
||||
|
||||
int prev = head->value;
|
||||
int index = 0;
|
||||
ret.push_back(vector<int>(1, prev));
|
||||
|
||||
ListNode *temp = head->next;
|
||||
|
||||
while (!temp)
|
||||
{
|
||||
if (prev == temp->value)
|
||||
{
|
||||
index++;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
if (index >= ret.size())
|
||||
{
|
||||
ret.push_back(vector<int>(1, temp->value));
|
||||
}
|
||||
if (index < ret.size())
|
||||
{
|
||||
ret[index].push_back(temp->value);
|
||||
}
|
||||
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include "../structs/Tree.h"
|
||||
|
||||
pair<bool, int> help(TreeNode *root)
|
||||
{
|
||||
if (!root)
|
||||
{
|
||||
return {true, 0};
|
||||
}
|
||||
auto p1 = help(root->left);
|
||||
auto p2 = help(root->right);
|
||||
if (!p1.first || !p2.first)
|
||||
{
|
||||
return {false, 0};
|
||||
}
|
||||
if (abs(p1.second - p2.second) > 1)
|
||||
{
|
||||
return {false, 0};
|
||||
}
|
||||
|
||||
return {true, max(p1.second, p2.second) + 1};
|
||||
}
|
||||
|
||||
bool test2(TreeNode *root)
|
||||
{
|
||||
return help(root).first;
|
||||
}
|
||||
21
else/topic/2023.h
Normal file
21
else/topic/2023.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <vector>
|
||||
#include<stack>
|
||||
using namespace std;
|
||||
|
||||
bool test1(vector<int> array){
|
||||
stack<int> S;
|
||||
for (int i = 0; i < array.size()/2; i++)
|
||||
{
|
||||
S.push(array[i]);
|
||||
}
|
||||
int tag = array.size()%2;
|
||||
for(int i = array.size()/2 + tag;i<array.size();i++){
|
||||
if (S.top() != array[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
S.pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
58
else/topic/2024.h
Normal file
58
else/topic/2024.h
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/12/21.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2024_H
|
||||
#define LEECODE_C_2024_H
|
||||
|
||||
#include "stack"
|
||||
|
||||
struct Node {
|
||||
int value;
|
||||
struct Node *left;
|
||||
struct Node *right;
|
||||
bool tag1;
|
||||
bool tag2;
|
||||
};
|
||||
|
||||
using namespace std;
|
||||
|
||||
int calculate(stack<Node *> S) {
|
||||
stack<Node *> temp;
|
||||
int num = 0;
|
||||
int i = 0;
|
||||
while (!S.empty()) {
|
||||
num += S.top()->value * pow(10, i++);
|
||||
S.pop();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int get(Node *root) {
|
||||
if (!root) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
stack<Node *> S;
|
||||
S.push(root);
|
||||
while (!S.empty()) {
|
||||
if (!S.top()->left && !S.top()->right) {
|
||||
count += calculate(S);
|
||||
}
|
||||
if (S.top()->tag1 == 0 && S.top()->left) {
|
||||
S.top()->tag1 = 1;
|
||||
S.push(S.top()->left);
|
||||
continue;
|
||||
}
|
||||
if (S.top()->tag2 == 0 && S.top()->left) {
|
||||
S.top()->tag2 = 1;
|
||||
S.push(S.top()->right);
|
||||
continue;
|
||||
}
|
||||
S.pop();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
#endif //LEECODE_C_2024_H
|
||||
Reference in New Issue
Block a user