mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 01:06:33 +08:00
init
This commit is contained in:
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
.idea
|
||||
|
||||
.vscode
|
||||
|
||||
.DS_Store
|
||||
|
||||
**/.DS_Store
|
||||
|
||||
.DS_Store?
|
||||
|
||||
cmake-build-debug
|
||||
CMakeFiles
|
||||
build
|
||||
cmake_install.cmake
|
||||
CMakeCache.txt
|
||||
Makefile
|
||||
main
|
||||
50
23/04/1019.c
Normal file
50
23/04/1019.c
Normal file
@@ -0,0 +1,50 @@
|
||||
struct ListNode
|
||||
{
|
||||
int val;
|
||||
struct ListNode *next;
|
||||
};
|
||||
|
||||
typedef struct Pair {
|
||||
int first;
|
||||
int second;
|
||||
} Pair;
|
||||
|
||||
int getLength(struct ListNode* head){
|
||||
int len = 0;
|
||||
ListNode* node = head;
|
||||
while (node.next)
|
||||
{
|
||||
node = node.next;
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int* nextLargerNodes(struct ListNode* head, int* returnSize){
|
||||
int len = 0;
|
||||
struct ListNode* cur = head;
|
||||
while (cur) {
|
||||
cur = cur->next;
|
||||
len++;
|
||||
}
|
||||
int* ans = (int *)calloc(len, sizeof(int));
|
||||
Pair stack[len];
|
||||
int top = 0, pos = 0;
|
||||
|
||||
cur = head;
|
||||
int idx = -1;
|
||||
while (cur) {
|
||||
++idx;
|
||||
ans[pos++] = 0;
|
||||
while (top > 0 && stack[top - 1].first < cur->val) {
|
||||
ans[stack[top - 1].second] = cur->val;
|
||||
top--;
|
||||
}
|
||||
stack[top].first = cur->val;
|
||||
stack[top].second = idx;
|
||||
top++;
|
||||
cur = cur->next;
|
||||
}
|
||||
*returnSize = len;
|
||||
return ans;
|
||||
}
|
||||
20
23/04/2399.c
Normal file
20
23/04/2399.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
bool checkDistances(char *s, int *distance, int distanceSize)
|
||||
{
|
||||
for (int i = 0; i < strlen(s); i++)
|
||||
{
|
||||
int site = s[i] - 'a';
|
||||
if (distance[i] == -1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (site + distance[i] + 1 >= strlen(s) || s[i] != s[i + distance[site] + 1])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
distance[i] = -1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
57
23/05/1373.c
Normal file
57
23/05/1373.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
const int INF = 0x3f3f3f3f;
|
||||
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
};
|
||||
|
||||
typedef struct SubTree {
|
||||
bool isBST;
|
||||
int minValue;
|
||||
int maxValue;
|
||||
int sumValue;
|
||||
} SubTree;
|
||||
|
||||
SubTree *createSubTree(bool isBST, int minValue,int maxValue, int sumValue) {
|
||||
SubTree *obj = (SubTree *)malloc(sizeof(SubTree));
|
||||
obj->isBST = isBST;
|
||||
obj->minValue = minValue;
|
||||
obj->maxValue = maxValue;
|
||||
obj->sumValue = sumValue;
|
||||
return obj;
|
||||
}
|
||||
|
||||
SubTree* dfs(struct TreeNode* root, int *res) {
|
||||
if (root == NULL) {
|
||||
return createSubTree(true, INF, -INF, 0);
|
||||
}
|
||||
SubTree *left = dfs(root->left, res);
|
||||
SubTree *right = dfs(root->right, res);
|
||||
SubTree *ret = NULL;
|
||||
|
||||
if (left->isBST && right->isBST &&
|
||||
root->val > left->maxValue &&
|
||||
root->val < right->minValue) {
|
||||
int sum = root->val + left->sumValue + right->sumValue;
|
||||
*res = MAX(*res, sum);
|
||||
ret = createSubTree(true, MIN(left->minValue, root->val), \
|
||||
MAX(root->val, right->maxValue), sum);
|
||||
} else {
|
||||
ret = createSubTree(false, 0, 0, 0);
|
||||
}
|
||||
free(left);
|
||||
free(right);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int maxSumBST(struct TreeNode* root){
|
||||
int res = 0;
|
||||
SubTree *obj = dfs(root, &res);
|
||||
free(obj);
|
||||
return res;
|
||||
}
|
||||
26
23/05/33.c
Normal file
26
23/05/33.c
Normal file
@@ -0,0 +1,26 @@
|
||||
static int max(int a, int b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
static int min(int a, int b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
int storeWater(int* bucket, int bucketSize, int* vat, int vatSize) {
|
||||
int maxk = 0;
|
||||
for (int i = 0; i < vatSize; i++) {
|
||||
maxk = max(maxk, vat[i]);
|
||||
}
|
||||
if (maxk == 0) {
|
||||
return 0;
|
||||
}
|
||||
int res = INT_MAX;
|
||||
for (int k = 1; k <= maxk && k < res; ++k) {
|
||||
int t = 0;
|
||||
for (int i = 0; i < bucketSize; ++i) {
|
||||
t += max(0, (vat[i] + k - 1) / k - bucket[i]);
|
||||
}
|
||||
res = min(res, t + k);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
22
23/07/2208.cpp
Normal file
22
23/07/2208.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <numeric>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int halveArray(vector<int>& nums) {
|
||||
priority_queue<double> pq(nums.begin(), nums.end());
|
||||
int res = 0;
|
||||
double sum = accumulate(nums.begin(), nums.end(), 0.0), sum2 = 0.0;
|
||||
while (sum2 < sum / 2) {
|
||||
double x = pq.top();
|
||||
pq.pop();
|
||||
sum2 += x / 2;
|
||||
pq.push(x / 2);
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
22
23/07/2500.cpp
Normal file
22
23/07/2500.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int deleteGreatestValue(vector<vector<int>>& grid) {
|
||||
int m = grid.size(), n = grid[0].size();
|
||||
for (int i = 0; i < m; i++) {
|
||||
sort(grid[i].begin(), grid[i].end());
|
||||
}
|
||||
int res = 0;
|
||||
for (int j = 0; j < n; j++) {
|
||||
int mx = 0;
|
||||
for (int i = 0; i < m; i++) {
|
||||
mx = max(mx, grid[i][j]);
|
||||
}
|
||||
res += mx;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
55
23/07/771.c
Normal file
55
23/07/771.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "math.h"
|
||||
#include "stdio.h"
|
||||
|
||||
// 未验证,leecode使用了 uthash.h 库进行处理
|
||||
typedef struct linked {
|
||||
char val;
|
||||
struct linked *next;
|
||||
} *HashTable;
|
||||
|
||||
HashTable *createTable(int hashCode, const char *jewels) {
|
||||
HashTable table[hashCode];
|
||||
for (int i = 0; i < sizeof(jewels) / sizeof(jewels[0]); ++i) {
|
||||
int temp = jewels[i] % hashCode;
|
||||
struct linked link = {jewels[i], NULL};
|
||||
if (table[temp] == NULL) {
|
||||
table[temp] = &link;
|
||||
} else {
|
||||
HashTable temps = table[temp];
|
||||
if (temps->val == jewels[i]) {
|
||||
continue;
|
||||
}
|
||||
while (temps->next != NULL) {
|
||||
temps = temps->next;
|
||||
if (temps->val == jewels[i]) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
temps->next = &link;
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
int contained(char x, HashTable *table, int hashCode) {
|
||||
int temp = x % hashCode;
|
||||
HashTable temps = table[temp];
|
||||
while (temps){
|
||||
if (temps->val == x){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int numJewelsInStones(char *jewels, char *stones) {
|
||||
int count = 0;
|
||||
int hashCode = (int) sqrt(sizeof(jewels) / sizeof(jewels[0])) + 1;
|
||||
HashTable *table = createTable(hashCode, jewels);
|
||||
for (int i = 0; i < sizeof(stones) / sizeof(stones[0]); ++i) {
|
||||
if (contained(stones[i], table, hashCode)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
14
23/08/1281.cpp
Normal file
14
23/08/1281.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
class Q1281 {
|
||||
public:
|
||||
int subtractProductAndSum(int n) {
|
||||
int plus = 0;
|
||||
int mult = 1;
|
||||
while (n) {
|
||||
int quo = n % 10;
|
||||
n = n / 10;
|
||||
plus += quo;
|
||||
mult *= quo;
|
||||
}
|
||||
return mult - plus;
|
||||
}
|
||||
};
|
||||
9
23/08/1749.cpp
Normal file
9
23/08/1749.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "vector"
|
||||
using namespace std;
|
||||
|
||||
class Q1749 {
|
||||
public:
|
||||
int maxAbsoluteSum(vector<int>& nums) {
|
||||
|
||||
}
|
||||
};
|
||||
38
23/08/21.cpp
Normal file
38
23/08/21.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
class Q21
|
||||
{
|
||||
public:
|
||||
struct ListNode
|
||||
{
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
|
||||
{
|
||||
ListNode *preHead = new ListNode(-1);
|
||||
|
||||
ListNode *prev = preHead;
|
||||
while (l1 != nullptr && l2 != nullptr)
|
||||
{
|
||||
if (l1->val < l2->val)
|
||||
{
|
||||
prev->next = l1;
|
||||
l1 = l1->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev->next = l2;
|
||||
l2 = l2->next;
|
||||
}
|
||||
prev = prev->next;
|
||||
}
|
||||
|
||||
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
|
||||
prev->next = l1 == nullptr ? l2 : l1;
|
||||
std::cout << "123";
|
||||
return preHead->next;
|
||||
}
|
||||
};
|
||||
28
23/08/24.cpp
Normal file
28
23/08/24.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include "lists.h"
|
||||
|
||||
class Q24 {
|
||||
public:
|
||||
ListNode *swapPairs(ListNode *head) {
|
||||
if (!head || !head->next) {
|
||||
return head;
|
||||
}
|
||||
ListNode *p1;
|
||||
ListNode *p2;
|
||||
p1 = head;
|
||||
p2 = head->next;
|
||||
|
||||
while (!p2){
|
||||
p1->next = p2->next;
|
||||
p2->next = p1;
|
||||
|
||||
if(!p1->next){
|
||||
return head;
|
||||
}
|
||||
|
||||
p1 = p1->next;
|
||||
p2 = p1->next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
};
|
||||
13
23/08/Q1388.cpp
Normal file
13
23/08/Q1388.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/18.
|
||||
//
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q1388 {
|
||||
public:
|
||||
int maxSizeSlices(vector<int> &slices) {
|
||||
|
||||
}
|
||||
};
|
||||
17
23/08/Q1572.cpp
Normal file
17
23/08/Q1572.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q1572 {
|
||||
public:
|
||||
int diagonalSum_1(vector<vector<int>> &mat) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < mat.size(); ++i) {
|
||||
sum += mat[i][i] + mat[i][mat.size() - i - 1];
|
||||
}
|
||||
if ((mat.size() & 1) == 1) {
|
||||
sum -= mat[mat.size() / 2][mat.size() / 2];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
14
23/08/Q2235.cpp
Normal file
14
23/08/Q2235.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/19.
|
||||
//
|
||||
class Q2235 {
|
||||
public:
|
||||
int sum(int num1, int num2) {
|
||||
while (num2) {
|
||||
unsigned int carry = (unsigned int) (num1 & num2) << 1;
|
||||
num1 ^= num2;
|
||||
num2 = carry;
|
||||
}
|
||||
return num1;
|
||||
}
|
||||
};
|
||||
15
23/08/Q23.cpp
Normal file
15
23/08/Q23.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/12.
|
||||
//
|
||||
#include "../../dataStruct/LinkedList/lists.h"
|
||||
|
||||
using namespace std;
|
||||
class Q23 {
|
||||
public:
|
||||
bool compare(const ListNode *a, const ListNode *b) {
|
||||
return a->val < b->val;
|
||||
}
|
||||
|
||||
ListNode* mergeKLists(vector<ListNode*>& lists) {
|
||||
}
|
||||
};
|
||||
22
23/08/Q2682.cpp
Normal file
22
23/08/Q2682.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/16.
|
||||
//
|
||||
#include "vector"
|
||||
using namespace std;
|
||||
class Q2682 {
|
||||
public:
|
||||
vector<int> circularGameLosers(int n, int k) {
|
||||
vector<bool> visit(n, false);
|
||||
for (int i = k, j = 0; !visit[j]; i += k) {
|
||||
visit[j] = true;
|
||||
j = (j + i) % n;
|
||||
}
|
||||
vector<int> ans;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!visit[i]) {
|
||||
ans.emplace_back(i + 1);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
20
23/08/Q617.cpp
Normal file
20
23/08/Q617.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/14.
|
||||
//
|
||||
#include "../../dataStruct/Tree/Tree.h"
|
||||
|
||||
class Q617 {
|
||||
public:
|
||||
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
|
||||
if (root1 == nullptr) {
|
||||
return root2;
|
||||
}
|
||||
if (root2 == nullptr) {
|
||||
return root1;
|
||||
}
|
||||
auto merged = new TreeNode(root1->val + root2->val);
|
||||
merged->left = mergeTrees(root1->left, root2->left);
|
||||
merged->right = mergeTrees(root1->right, root2->right);
|
||||
return merged;
|
||||
}
|
||||
};
|
||||
28
23/08/Q88.cpp
Normal file
28
23/08/Q88.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/13.
|
||||
//
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
class Q88 {
|
||||
public:
|
||||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
|
||||
int p1 = 0, p2 = 0;
|
||||
int sorted[m + n];
|
||||
int cur;
|
||||
while (p1 < m || p2 < n) {
|
||||
if (p1 == m) {
|
||||
cur = nums2[p2++];
|
||||
} else if (p2 == n) {
|
||||
cur = nums1[p1++];
|
||||
} else if (nums1[p1] < nums2[p2]) {
|
||||
cur = nums1[p1++];
|
||||
} else {
|
||||
cur = nums2[p2++];
|
||||
}
|
||||
sorted[p1 + p2 - 1] = cur;
|
||||
}
|
||||
for (int i = 0; i != m + n; ++i) {
|
||||
nums1[i] = sorted[i];
|
||||
}
|
||||
}
|
||||
};
|
||||
16
23/09/2594.h
Normal file
16
23/09/2594.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/9/7.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2594_H
|
||||
#define LEECODE_C_2594_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long long repairCars(vector<int> &ranks, int cars) {
|
||||
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_2594_H
|
||||
34
23/09/Q1123.h
Normal file
34
23/09/Q1123.h
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/9/6.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1123_H
|
||||
#define LEECODE_C_Q1123_H
|
||||
#pragma once
|
||||
|
||||
#include "../../dataStruct/Tree/TreeStack.h"
|
||||
|
||||
std::pair<TreeNode *, int> f(TreeNode *root) {
|
||||
if (!root) {
|
||||
return {root, 0};
|
||||
}
|
||||
|
||||
auto left = f(root->left);
|
||||
auto right = f(root->right);
|
||||
|
||||
if (left.second > right.second) {
|
||||
return {left.first, left.second + 1};
|
||||
}
|
||||
if (left.second < right.second) {
|
||||
return {right.first, right.second + 1};
|
||||
}
|
||||
return {root, left.second + 1};
|
||||
|
||||
}
|
||||
|
||||
struct TreeNode *lcaDeepestLeaves(struct TreeNode *root) {
|
||||
return f(root).first;
|
||||
}
|
||||
|
||||
|
||||
#endif //LEECODE_C_Q1123_H
|
||||
21
23/09/Q2240.h
Normal file
21
23/09/Q2240.h
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/9/1.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2240_H
|
||||
#define LEECODE_C_2240_H
|
||||
|
||||
class Q2240 {
|
||||
public:
|
||||
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
|
||||
long long count = total / cost2 + 1;
|
||||
int tos = total / cost1;
|
||||
for (int i = 1; i <= tos; ++i) {
|
||||
int temp = total - i * cost1;
|
||||
count += temp / cost2 + 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_2240_H
|
||||
26
23/10/2520.h
Normal file
26
23/10/2520.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/26.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2520_H
|
||||
#define LEECODE_C_2520_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2520 {
|
||||
public:
|
||||
int countDigits(int num) {
|
||||
string str = to_string(num);
|
||||
int res = 0;
|
||||
for (auto &ch: str) {
|
||||
if (num % (ch - '0') == 0) {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_2520_H
|
||||
49
23/10/901.h
Normal file
49
23/10/901.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/7.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q901_H
|
||||
#define LEECODE_C_Q901_H
|
||||
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class trendSpanner {
|
||||
public:
|
||||
trendSpanner() {}
|
||||
|
||||
int next(int price) {
|
||||
count = 1;
|
||||
|
||||
if (trend.size() == 0 || price < trend.top()->value) {
|
||||
trend.push(new stock{price, 1});
|
||||
return count;
|
||||
}
|
||||
|
||||
while (!trend.empty() && price >= trend.top()->value) {
|
||||
count += trend.top()->value;
|
||||
stock *temp = trend.top();
|
||||
trend.pop();
|
||||
delete (temp);
|
||||
}
|
||||
|
||||
trend.push(new stock{price, count});
|
||||
return count;
|
||||
}
|
||||
|
||||
private:
|
||||
struct stock {
|
||||
int key;
|
||||
int value;
|
||||
};
|
||||
stack<stock *> trend;
|
||||
int count;
|
||||
};
|
||||
/**
|
||||
* Your trendSpanner object will be instantiated and called as such:
|
||||
* trendSpanner* obj = new trendSpanner();
|
||||
* int param_1 = obj->next(price);
|
||||
*/
|
||||
|
||||
#endif LEECODE_C_Q901_H
|
||||
25
23/10/Q1346.h
Normal file
25
23/10/Q1346.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef LEECODE_C_Q1346_H
|
||||
#define LEECODE_C_Q1346_H
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
class Q1346 {
|
||||
public:
|
||||
bool checkIfExist(vector<int>& arr) {
|
||||
unordered_map<int,int> m;
|
||||
for(auto i:arr){
|
||||
m[i] = 1;
|
||||
}
|
||||
|
||||
for(auto [key,value]:m){
|
||||
if (m[key*2] == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif LEECODE_C_Q1346_H
|
||||
23
23/10/Q136.h
Normal file
23
23/10/Q136.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/14.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q136_H
|
||||
#define LEECODE_C_Q136_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q136 {
|
||||
public:
|
||||
int singleNumber(vector<int> &nums) {
|
||||
int count = 0;
|
||||
for (int i: nums) {
|
||||
count ^= i;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q136_H
|
||||
29
23/10/Q137.h
Normal file
29
23/10/Q137.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/16.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q137_H
|
||||
#define LEECODE_C_Q137_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q137 {
|
||||
public:
|
||||
int singleNumber(vector<int> &nums) {
|
||||
unordered_map<int, int> m;
|
||||
for (int i: nums) {
|
||||
m[i] = m[i] + 1;
|
||||
}
|
||||
for (auto [num, count]: m) {
|
||||
if (count == 1) {
|
||||
return num;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q137_H
|
||||
41
23/10/Q1465.h
Normal file
41
23/10/Q1465.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/27.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1465_H
|
||||
#define LEECODE_C_Q1465_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q1465 {
|
||||
public:
|
||||
int maxArea(int h, int w, vector<int> &horizontalCuts, vector<int> &verticalCuts) {
|
||||
int maxHorizon;
|
||||
int maxVertical;
|
||||
|
||||
if (horizontalCuts.size()) {
|
||||
maxHorizon = max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.size() - 1]);
|
||||
} else {
|
||||
maxHorizon = h;
|
||||
}
|
||||
if (verticalCuts.size()) {
|
||||
maxVertical = max(verticalCuts[0], w - verticalCuts[verticalCuts.size() - 1]);
|
||||
} else {
|
||||
maxVertical = w;
|
||||
}
|
||||
|
||||
for (int i = 1; i < horizontalCuts.size(); ++i) {
|
||||
maxHorizon = max(maxHorizon, horizontalCuts[i] - horizontalCuts[i - 1]);
|
||||
}
|
||||
|
||||
for (int i = 1; i < verticalCuts.size(); ++i) {
|
||||
maxVertical = max(maxVertical, verticalCuts[i] - verticalCuts[i - 1]);
|
||||
}
|
||||
|
||||
return (long long) maxHorizon * maxVertical % 1000000001;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q1465_H
|
||||
44
23/10/Q1488.h
Normal file
44
23/10/Q1488.h
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/13.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1488_H
|
||||
#define LEECODE_C_Q1488_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q1488 {
|
||||
public:
|
||||
vector<int> avoidFlood(vector<int> &rains) {
|
||||
map<int, int> m;
|
||||
vector<int> ret;
|
||||
for (int i = 0; i < rains.size(); i++) {
|
||||
if (rains[i] == 0) {
|
||||
for (int j = i + 1; j < rains.size(); ++j) {
|
||||
if (j > 0) {
|
||||
if (m[rains[j]] == 1) {
|
||||
m.erase(rains[j]);
|
||||
ret.push_back(rains[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == rains.size() - 1) {
|
||||
ret.push_back(1);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (m[rains[i]]) {
|
||||
return vector<int>();
|
||||
}
|
||||
m[rains[i]] = 1;
|
||||
ret.push_back(-1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q1488_H
|
||||
77
23/10/Q1726.h
Normal file
77
23/10/Q1726.h
Normal file
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/19.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q1726_H
|
||||
#define LEECODE_C_Q1726_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <../../dataStruct/Combination.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int tupleSameProduct(vector<int> &nums) {
|
||||
int count = 0;
|
||||
unordered_map<int, int> m;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
for (int j = i + 1; j < nums.size(); ++j) {
|
||||
m[nums[i] * nums[j]] = m[nums[i] * nums[j]] + 1;
|
||||
}
|
||||
}
|
||||
for (auto &pair: m) {
|
||||
if (pair.second > 1) {
|
||||
count += pair.second * (pair.second - 1) * 4;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int tupleSameProduct2(vector<int> &nums) {
|
||||
int count = 0;
|
||||
vector<int> result;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
for (int j = i + 1; j < nums.size(); ++j) {
|
||||
result.push_back(nums[i] * nums[j]);
|
||||
}
|
||||
}
|
||||
sort(result.begin(), result.end());
|
||||
int temp = 0;
|
||||
int num = nums[0];
|
||||
for (int i = 1; i < result.size(); ++i) {
|
||||
if (result[i] == num) {
|
||||
count += (temp++) * 8;
|
||||
} else {
|
||||
num = result[i];
|
||||
temp = 0;
|
||||
}
|
||||
if (result[i] == result[i - 1]) {
|
||||
count += 8;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int tupleSameProduct3(vector<int> &nums) {
|
||||
int count = 0;
|
||||
vector<int> result;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
for (int j = i + 1; j < nums.size(); ++j) {
|
||||
result.push_back(nums[i] * nums[j]);
|
||||
}
|
||||
}
|
||||
sort(result.begin(), result.end());
|
||||
int temp = 1;
|
||||
int num = nums[0];
|
||||
for (int i = 1; i < result.size(); ++i) {
|
||||
if (result[i] == num) {
|
||||
temp++;
|
||||
} else {
|
||||
count += temp * (temp - 1) * 4;
|
||||
num = result[i];
|
||||
temp = 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
#endif //LEECODE_C_Q1726_H
|
||||
29
23/10/Q2525.h
Normal file
29
23/10/Q2525.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/20.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2525_H
|
||||
#define LEECODE_C_Q2525_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2525 {
|
||||
public:
|
||||
string categorizeBox(int length, int width, int height, int mass) {
|
||||
if (length >= 10000 || width >= 10000 || height >= 10000 || (long long)length * width * height >= 1000000000) {
|
||||
if (mass >= 100) {
|
||||
return "Both";
|
||||
} else {
|
||||
return "Bulky";
|
||||
}
|
||||
}
|
||||
if (mass >= 100) {
|
||||
return "Heavy";
|
||||
}
|
||||
return "Neither";
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2525_H
|
||||
33
23/10/Q2526.h
Normal file
33
23/10/Q2526.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/12.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2526_H
|
||||
#define LEECODE_C_Q2526_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2526 {
|
||||
public:
|
||||
long long findTheArrayConcVal(vector<int> &nums) {
|
||||
long long ans = 0;
|
||||
for (int i = 0, j = nums.size() - 1; i <= j; i++, j--) {
|
||||
if (i != j) {
|
||||
int temp = nums[j];
|
||||
int digit = 0;
|
||||
while (temp != 0) {
|
||||
digit++;
|
||||
temp /= 10;
|
||||
}
|
||||
ans += nums[j] + (long long) nums[i] * pow(10, digit);
|
||||
} else {
|
||||
ans += nums[i];
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2526_H
|
||||
26
23/10/Q2530.h
Normal file
26
23/10/Q2530.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/18.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2530_H
|
||||
#define LEECODE_C_Q2530_H
|
||||
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long long maxKelements(vector<int> &nums, int k) {
|
||||
long long count = 0;
|
||||
priority_queue<int> Q(nums.begin(), nums.end());
|
||||
while (k) {
|
||||
auto top = Q.top();
|
||||
Q.pop();
|
||||
count += top;
|
||||
Q.push(ceil(top / 3.0));
|
||||
k--;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q2530_H
|
||||
35
23/10/Q260.h
Normal file
35
23/10/Q260.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/16.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q260_H
|
||||
#define LEECODE_C_Q260_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q260 {
|
||||
public:
|
||||
vector<int> singleNumber(vector<int> &nums) {
|
||||
unordered_map<int, int> m;
|
||||
for (int i: nums) {
|
||||
m[i] = m[i] + 1;
|
||||
}
|
||||
vector<int> ret;
|
||||
for (auto [num, count]: m) {
|
||||
if (count == 1) {
|
||||
if (ret.empty()) {
|
||||
ret.push_back(num);
|
||||
} else {
|
||||
ret.push_back(num);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q260_H
|
||||
19
23/10/Q2652.h
Normal file
19
23/10/Q2652.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/17.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2652_H
|
||||
#define LEECODE_C_Q2652_H
|
||||
|
||||
class Q2652 {
|
||||
public:
|
||||
int f(int n, int m) {
|
||||
return (m + n / m * m) * (n / m) / 2;
|
||||
}
|
||||
|
||||
int sumOfMultiples(int n) {
|
||||
return f(n, 3) + f(n, 5) + f(n, 7) - f(n, 3 * 5) - f(n, 3 * 7) - f(n, 5 * 7) + f(n, 3 * 5 * 7);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2652_H
|
||||
26
23/10/Q2698.h
Normal file
26
23/10/Q2698.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/25.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2698_H
|
||||
#define LEECODE_C_Q2698_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q2698 {
|
||||
public:
|
||||
int punishmentNumber(int n) {
|
||||
auto all = vector<int>(
|
||||
{1, 9, 10, 36, 45, 55, 82, 91, 99, 100, 235, 297, 369, 370, 379, 414, 657, 675, 703, 756, 792, 909, 918,
|
||||
945, 964, 990, 991, 999, 1000});
|
||||
int count = 0;
|
||||
for (int i = 0; i < all.size() && all[i] <= n; ++i) {
|
||||
count += all[i] * all[i];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q2698_H
|
||||
30
23/10/Q274.h
Normal file
30
23/10/Q274.h
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/29.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q274_H
|
||||
#define LEECODE_C_Q274_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q274 {
|
||||
public:
|
||||
int hIndex(vector<int> &citations) {
|
||||
sort(citations.begin(), citations.end(), [](int a, int b) {
|
||||
return a > b;
|
||||
});
|
||||
for (int i = 1; i < citations.size(); ++i) {
|
||||
if (citations[i - 1] < i) {
|
||||
return i - 1;
|
||||
}
|
||||
if (i + 1 == citations.size() - 1) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return citations[0] == 0 ? 0 : 1;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q274_H
|
||||
29
23/10/Q275.h
Normal file
29
23/10/Q275.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/30.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q275_H
|
||||
#define LEECODE_C_Q275_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Q275 {
|
||||
public:
|
||||
int hIndex(vector<int> &citations) {
|
||||
int n = citations.size();
|
||||
int left = 0, right = n - 1;
|
||||
while (left <= right) {
|
||||
int mid = left + (right - left) / 2;
|
||||
if (citations[mid] >= n - mid) {
|
||||
right = mid - 1;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
return n - left;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_Q275_H
|
||||
57
23/10/T368.h
Normal file
57
23/10/T368.h
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/22.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_T368_H
|
||||
#define LEECODE_C_T368_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int minimumSum(vector<int> &nums) {
|
||||
int left = 0;
|
||||
int right = nums.size() - 1;
|
||||
for (int i = 0; i < right; ++i) {
|
||||
if (nums[left] >= nums[i]) {
|
||||
left = i;
|
||||
}
|
||||
}
|
||||
for (int i = right; i > left; --i) {
|
||||
if (nums[right] >= nums[i]) {
|
||||
right = i;
|
||||
}
|
||||
}
|
||||
if (right - left == 1) {
|
||||
return -1;
|
||||
}
|
||||
int max = left + 1;
|
||||
for (int i = left + 1; i < right; ++i) {
|
||||
if (nums[max] >= nums[i]) {
|
||||
max = i;
|
||||
}
|
||||
}
|
||||
return nums[left] + nums[right] + nums[max];
|
||||
}
|
||||
|
||||
void runT368() {
|
||||
vector<int> arr; // [5,4,8,7,10,2] / [6,5,4,3,4,5]
|
||||
/* arr.push_back(5);
|
||||
arr.push_back(4);
|
||||
arr.push_back(8);
|
||||
arr.push_back(7);
|
||||
arr.push_back(10);
|
||||
arr.push_back(2);*/
|
||||
// arr.push_back(6);
|
||||
// arr.push_back(5);
|
||||
// arr.push_back(4);
|
||||
// arr.push_back(3);
|
||||
// arr.push_back(4);
|
||||
// arr.push_back(5);
|
||||
arr.push_back(50);
|
||||
arr.push_back(50);
|
||||
arr.push_back(50);
|
||||
auto result = minimumSum(arr);
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_T368_H
|
||||
50
23/11/Q117.h
Normal file
50
23/11/Q117.h
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/11/3.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q117_H
|
||||
#define LEECODE_C_Q117_H
|
||||
|
||||
#include <stack>
|
||||
#include <queue>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Node {
|
||||
public:
|
||||
int val;
|
||||
Node *left;
|
||||
Node *right;
|
||||
Node *next;
|
||||
|
||||
Node() : val(0), left(nullptr), right(nullptr), next(nullptr) {}
|
||||
|
||||
Node(int _val) : val(_val), left(nullptr), right(nullptr), next(nullptr) {}
|
||||
|
||||
Node(int _val, Node *_left, Node *_right, Node *_next)
|
||||
: val(_val), left(_left), right(_right), next(_next) {}
|
||||
};
|
||||
|
||||
Node *connect(Node *root) {
|
||||
if (!root) {
|
||||
return nullptr;
|
||||
}
|
||||
queue<pair<Node *, int>> Q;
|
||||
Q.emplace(root, 1);
|
||||
while (!Q.empty()) {
|
||||
auto temp = Q.front();
|
||||
Q.pop();
|
||||
if (temp.second == Q.front().second) {
|
||||
temp.first->next = Q.front().first;
|
||||
}
|
||||
if (temp.first->left != nullptr) {
|
||||
Q.emplace(temp.first->left, temp.second + 1);
|
||||
}
|
||||
if (temp.first->right != nullptr) {
|
||||
Q.emplace(temp.first->right, temp.second + 1);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q117_H
|
||||
28
23/11/Q187.h
Normal file
28
23/11/Q187.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/11/5.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q187_H
|
||||
#define LEECODE_C_Q187_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<string> findRepeatedDnaSequences(string s) {
|
||||
vector<string> ret;
|
||||
unordered_map<string, int> m;
|
||||
for(int i = 0;i+9<s.length();i++){
|
||||
m[s.substr(i,i+9)] = m[s.substr(i,i+9)] + 1;
|
||||
}
|
||||
for(auto [key,value]:m){
|
||||
if(value > 1){
|
||||
ret.push_back(key);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q187_H
|
||||
28
23/11/Q2342.h
Normal file
28
23/11/Q2342.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef LEECODE_C_Q2342_H
|
||||
#define LEECODE_C_Q2342_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
int maximumSum(vector<int> &nums)
|
||||
{
|
||||
int ans = -1;
|
||||
int mx[82]{}; // 至多 9 个 9 相加
|
||||
for (int num : nums)
|
||||
{
|
||||
int s = 0; // num 的数位和
|
||||
for (int x = num; x; x /= 10)
|
||||
{ // 枚举 num 的每个数位
|
||||
s += x % 10;
|
||||
}
|
||||
if (mx[s])
|
||||
{ // 说明左边也有数位和等于 s 的元素
|
||||
ans = max(ans, mx[s] + num); // 更新答案的最大值
|
||||
}
|
||||
mx[s] = max(mx[s], num); // 维护数位和等于 s 的最大元素
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
#endif LEECODE_C_Q2342_H
|
||||
31
23/11/Q2586.h
Normal file
31
23/11/Q2586.h
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/11/8.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2586_H
|
||||
#define LEECODE_C_Q2586_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int vowelStrings(vector<string> &words, int left, int right) {
|
||||
unordered_map<char, int> m;
|
||||
m['a'] = 1;
|
||||
m['e'] = 1;
|
||||
m['i'] = 1;
|
||||
m['o'] = 1;
|
||||
m['u'] = 1;
|
||||
int count = 0;
|
||||
for (int i = left; i <= right; ++i) {
|
||||
auto s = words[i];
|
||||
if (s != "" && m[s[0]] && m[s[s.length() - 1]]) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q2586_H
|
||||
25
23/11/Q2609.h
Normal file
25
23/11/Q2609.h
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/11/8.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q2609_H
|
||||
#define LEECODE_C_Q2609_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int findTheLongestBalancedSubstring(string s) {
|
||||
int n = s.size(), idx = 0, ans = 0;
|
||||
while (idx < n) {
|
||||
int a = 0, b = 0;
|
||||
while (idx < n && s[idx] == '0' && ++a >= 0) idx++;
|
||||
while (idx < n && s[idx] == '1' && ++b >= 0) idx++;
|
||||
ans = max(ans, min(a, b) * 2);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q2609_H
|
||||
17
23/11/Q318.h
Normal file
17
23/11/Q318.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/11/6.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q318_H
|
||||
#define LEECODE_C_Q318_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int maxProduct(vector<string> &words) {
|
||||
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q318_H
|
||||
33
23/11/Q876.h
Normal file
33
23/11/Q876.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/11/7.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_Q876_H
|
||||
#define LEECODE_C_Q876_H
|
||||
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
ListNode *middleNode(ListNode *head) {
|
||||
ListNode *slow = head, *fast = head;
|
||||
while (fast && fast->next) {
|
||||
slow = slow->next;
|
||||
fast = fast->next->next;
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_Q876_H
|
||||
35
24/01/2497.h
Normal file
35
24/01/2497.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/3.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2497_H
|
||||
#define LEECODE_C_2497_H
|
||||
|
||||
#include "../../dataStruct/LinkedList/lists.h"
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//单调栈
|
||||
ListNode *removeNodes(ListNode *head) {
|
||||
stack<ListNode *> S;
|
||||
S.push(head);
|
||||
ListNode *temp = head->next;
|
||||
while (temp) {
|
||||
while (!S.empty() && temp->val > S.top()->val) {
|
||||
S.pop();
|
||||
}
|
||||
S.push(temp);
|
||||
temp = temp->next;
|
||||
}
|
||||
temp = S.top();
|
||||
S.pop();
|
||||
while (!S.empty()) {
|
||||
S.top()->next = temp;
|
||||
temp = S.top();
|
||||
S.pop();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_2497_H
|
||||
33
24/01/670.h
Normal file
33
24/01/670.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/22.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_670_H
|
||||
#define LEECODE_C_670_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int maximumSwap(int num) {
|
||||
string charArray = to_string(num);
|
||||
int n = charArray.size();
|
||||
int maxIdx = n - 1;
|
||||
int idx1 = -1, idx2 = -1;
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
if (charArray[i] > charArray[maxIdx]) {
|
||||
maxIdx = i;
|
||||
} else if (charArray[i] < charArray[maxIdx]) {
|
||||
idx1 = i;
|
||||
idx2 = maxIdx;
|
||||
}
|
||||
}
|
||||
if (idx1 >= 0) {
|
||||
swap(charArray[idx1], charArray[idx2]);
|
||||
return stoi(charArray);
|
||||
} else {
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_670_H
|
||||
45
24/01/82.h
Normal file
45
24/01/82.h
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/15.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_82_H
|
||||
#define LEECODE_C_82_H
|
||||
|
||||
// 本题与83题中要求删除重复多余的节点不同,要求只要重复就删除掉所有重复的节点,关机在于链表头的处理
|
||||
// 这里采用的是创建一个新的头节点,然后对后续节点进行继续处理,一次遍历即可
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
ListNode *deleteDuplicates(ListNode *head) {
|
||||
if (!head) {
|
||||
return head;
|
||||
}
|
||||
|
||||
ListNode* dummy = new ListNode(0, head);
|
||||
|
||||
ListNode* cur = dummy;
|
||||
while (cur->next && cur->next->next) {
|
||||
if (cur->next->val == cur->next->next->val) {
|
||||
int x = cur->next->val;
|
||||
while (cur->next && cur->next->val == x) {
|
||||
cur->next = cur->next->next;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
return dummy->next;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_82_H
|
||||
19
24/02/235.h
Normal file
19
24/02/235.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/2/25.
|
||||
//
|
||||
|
||||
#ifndef LEETCODE_C_235_H
|
||||
#define LEETCODE_C_235_H
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
};
|
||||
|
||||
|
||||
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
|
||||
|
||||
}
|
||||
|
||||
#endif //LEETCODE_C_235_H
|
||||
82
24/02/429.h
Normal file
82
24/02/429.h
Normal file
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/2/17.
|
||||
//
|
||||
|
||||
#ifndef LEETCODE_C_429_H
|
||||
#define LEETCODE_C_429_H
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
struct Node {
|
||||
int val;
|
||||
int numChildren;
|
||||
struct Node **children;
|
||||
};
|
||||
|
||||
struct linkedNode {
|
||||
int val;
|
||||
struct linkedNode *next;
|
||||
};
|
||||
|
||||
struct queue {
|
||||
struct linkedNode *head;
|
||||
struct linkedNode *tail;
|
||||
int size;
|
||||
};
|
||||
|
||||
void push(struct queue Q, int val) {
|
||||
struct linkedNode *temp = (struct linkedNode *) malloc(sizeof(struct linkedNode));
|
||||
Q.size++;
|
||||
if (Q.size == 0) {
|
||||
temp->val = val;
|
||||
Q.head = temp;
|
||||
Q.tail = temp;
|
||||
return;
|
||||
}
|
||||
Q.tail->next = temp;
|
||||
Q.tail = temp;
|
||||
}
|
||||
|
||||
int pop(struct queue Q) {
|
||||
int top = Q.head->val;
|
||||
struct linkedNode *temp = Q.head;
|
||||
Q.head = temp->next;
|
||||
free(temp);
|
||||
Q.size--;
|
||||
return top;
|
||||
}
|
||||
|
||||
int top(struct queue Q) {
|
||||
return Q.head->val;
|
||||
}
|
||||
|
||||
int **levelOrder(struct Node *root, int *returnSize, int **returnColumnSizes) {
|
||||
int ** ans = (int **)malloc(sizeof(int *) * 1000);
|
||||
*returnColumnSizes = (int *)malloc(sizeof(int) * 1000);
|
||||
if (!root) {
|
||||
*returnSize = 0;
|
||||
return ans;
|
||||
}
|
||||
struct Node ** queue = (struct Node **)malloc(sizeof(struct Node *) * 10000);
|
||||
int head = 0, tail = 0;
|
||||
int level = 0;
|
||||
queue[tail++] = root;
|
||||
|
||||
while (head != tail) {
|
||||
int cnt = tail - head;
|
||||
ans[level] = (int *)malloc(sizeof(int) * cnt);
|
||||
for (int i = 0; i < cnt; ++i) {
|
||||
struct Node * cur = queue[head++];
|
||||
ans[level][i] = cur->val;
|
||||
for (int j = 0; j < cur->numChildren; j++) {
|
||||
queue[tail++] = cur->children[j];
|
||||
}
|
||||
}
|
||||
(*returnColumnSizes)[level++] = cnt;
|
||||
}
|
||||
*returnSize = level;
|
||||
free(queue);
|
||||
return ans;
|
||||
}
|
||||
|
||||
#endif //LEETCODE_C_429_H
|
||||
41
24/02/590.h
Normal file
41
24/02/590.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/2/19.
|
||||
//
|
||||
|
||||
#ifndef LEETCODE_C_590_H
|
||||
#define LEETCODE_C_590_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
struct Node {
|
||||
int val;
|
||||
int numChildren;
|
||||
struct Node **children;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Note: The returned array must be malloced, assume caller calls free().
|
||||
*/
|
||||
#define MAX_NODE_SIZE 10000
|
||||
|
||||
void helper(const struct Node* root, int* res, int* pos) {
|
||||
if (NULL == root) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < root->numChildren; i++) {
|
||||
helper(root->children[i], res, pos);
|
||||
}
|
||||
res[(*pos)++] = root->val;
|
||||
}
|
||||
|
||||
int* postorder(struct Node* root, int* returnSize) {
|
||||
int * res = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
int pos = 0;
|
||||
helper(root, res, &pos);
|
||||
*returnSize = pos;
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif //LEETCODE_C_590_H
|
||||
40
24/03/322.c
Normal file
40
24/03/322.c
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/3/26.
|
||||
//
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int min(int a, int b)
|
||||
{
|
||||
return a > b ? b : a;
|
||||
}
|
||||
|
||||
int coinChange(int *coins, int coinsSize, int amount)
|
||||
{
|
||||
int *dp = (int *)calloc((amount + 1), sizeof(int));
|
||||
for (int i = 0; i <= amount; i++)
|
||||
{
|
||||
dp[i] = amount + 1;
|
||||
}
|
||||
dp[0] = 0;
|
||||
for (int i = 1; i <= amount; ++i)
|
||||
{
|
||||
for (int j = 0; j < coinsSize; ++j)
|
||||
{
|
||||
if (coins[j] <= i)
|
||||
{
|
||||
dp[i] = min(dp[i], dp[i - coins[j]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amount] > amount ? -1 : dp[amount];
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
int a1[3] = {1, 2, 5};
|
||||
int a2[4] = {3, 7, 405, 436};
|
||||
int a3[1] = {2};
|
||||
coinChange(a3, 1, 3);
|
||||
}
|
||||
27
24/03/518.c
Normal file
27
24/03/518.c
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/3/26.
|
||||
//
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int coinChange(int *coins, int coinsSize, int amount)
|
||||
{
|
||||
int *dp = (int *)calloc((amount + 1), sizeof(int));
|
||||
//memset(dp, 0, sizeof(dp));
|
||||
dp[0] = 1;
|
||||
for (int i = 0; i < coinsSize; i++) {
|
||||
for (int j = coins[i]; j <= amount; j++) {
|
||||
dp[j] += dp[j - coins[i]];
|
||||
}
|
||||
}
|
||||
return dp[amount];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a1[3] = {1, 2, 5};
|
||||
int a2[4] = {3, 7, 405, 436};
|
||||
int a3[1] = {2};
|
||||
coinChange(a3, 1, 3);
|
||||
}
|
||||
12
24/03/quicksort.c
Normal file
12
24/03/quicksort.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void swap(int* a, int* b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
void QuickSort(int *arr, int low, int high)
|
||||
{
|
||||
|
||||
}
|
||||
32
24/04/1379.cpp
Normal file
32
24/04/1379.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by szh2 on 24-4-3.
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
class Solution{
|
||||
public:
|
||||
TreeNode * getTargetCopy(TreeNode * original, TreeNode * cloned, TreeNode * target) {
|
||||
if (original == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (original == target) {
|
||||
return cloned;
|
||||
}
|
||||
TreeNode *left = getTargetCopy(original->left, cloned->left, target);
|
||||
if (left != nullptr) {
|
||||
return left;
|
||||
}
|
||||
return getTargetCopy(original->right, cloned->right, target);
|
||||
}
|
||||
};
|
||||
34
24/04/2192.cpp
Normal file
34
24/04/2192.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> getAncestors(int n, vector<vector<int>> &edges) {
|
||||
vector<vector<int>> g(n);
|
||||
for (auto &e : edges) {
|
||||
g[e[1]].push_back(e[0]); // 反向建图
|
||||
}
|
||||
|
||||
vector<vector<int>> ans(n);
|
||||
vector<int> vis(n);
|
||||
function<void(int)> dfs = [&](int x) {
|
||||
vis[x] = true; // 避免重复访问
|
||||
for (int y : g[x]) {
|
||||
if (!vis[y]) {
|
||||
dfs(y); // 只递归没有访问过的点
|
||||
}
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < n; i++) {
|
||||
ranges::fill(vis, false);
|
||||
dfs(i); // 从 i 开始 DFS
|
||||
vis[i] = false; // ans[i] 不含 i
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (vis[j]) {
|
||||
ans[i].push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
31
24/04/2810.c
Normal file
31
24/04/2810.c
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/4/3.
|
||||
//
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void reverse(char *s, int end) {
|
||||
int i = 0;
|
||||
while (i < end) {
|
||||
char temp = s[i];
|
||||
s[i] = s[end];
|
||||
s[end] = temp;
|
||||
i++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
|
||||
char *finalString(char *s) {
|
||||
int len = strlen(s);
|
||||
char *r = malloc((len + 1) * sizeof(char));
|
||||
int x = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0 && s[i] == 'i') {
|
||||
reverse(r, x - 1);
|
||||
} else {
|
||||
r[x++] = s[i];
|
||||
}
|
||||
}
|
||||
r[x] = '\0';
|
||||
return r;
|
||||
}
|
||||
39
24/06/520.c
Normal file
39
24/06/520.c
Normal file
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/6/23.
|
||||
//
|
||||
|
||||
#ifndef LEETCODE_C_520_CPP
|
||||
#define LEETCODE_C_520_CPP
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "ctype.h"
|
||||
#include "stdbool.h"
|
||||
#include "string.h"
|
||||
|
||||
char *toLowerCase(const char *str) {
|
||||
char *result = (char *) malloc(sizeof(char) * strlen(str));
|
||||
for (int i = 0; i < strlen(str); i++) {
|
||||
result[i]= tolower(str[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 通过数值判断
|
||||
bool detectCapitalUse(char *word) {
|
||||
char *lower = toLowerCase(word);
|
||||
int count = 0;
|
||||
for (int i = 0; i < strlen(word); ++i) {
|
||||
count += lower[i] - word[i];
|
||||
}
|
||||
int interval = 'a' - 'A';
|
||||
if ((count == interval && word[0] >= 'A' && word[0] <= 'Z') || count == interval * strlen(word) || count == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool run() {
|
||||
return detectCapitalUse("USA");
|
||||
}
|
||||
|
||||
#endif //LEETCODE_C_520_CPP
|
||||
30
24/06/LCP61.h
Normal file
30
24/06/LCP61.h
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/6/21.
|
||||
//
|
||||
|
||||
#ifndef LEETCODE_C_LCP61_H
|
||||
#define LEETCODE_C_LCP61_H
|
||||
|
||||
# include <math.h>
|
||||
|
||||
int getTemper(int *temperA, int i) {
|
||||
if (temperA[i + 1] == temperA[i]) {
|
||||
return 0;
|
||||
}
|
||||
return temperA[i + 1] < temperA[i] ? -1 : 1;
|
||||
}
|
||||
|
||||
int temperatureTrend(int *temperatureA, int temperatureASize, int *temperatureB, int temperatureBSize) {
|
||||
int ans = 0, count = 0;
|
||||
for (int i = 0; i+1 < temperatureASize; ++i) {
|
||||
int a = getTemper(temperatureA, i);
|
||||
int b = getTemper(temperatureB, i);
|
||||
if (a == b) {
|
||||
count++;
|
||||
ans = fmax(ans, count);
|
||||
} else count = 0;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
#endif //LEETCODE_C_LCP61_H
|
||||
22
24/09/2860.h
Normal file
22
24/09/2860.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int countWays(vector<int> &nums) {
|
||||
sort(nums.begin(), nums.end());
|
||||
int count = 0;
|
||||
for (int i = 0; i <= nums.size(); i++) {
|
||||
if (i == 0) {
|
||||
if (nums[0] != 0) {
|
||||
count++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (i == nums.size() && i > nums[nums.size()]) {
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
if (i > nums[i - 1] && i < nums[i])count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
13
24/09/70.h
Normal file
13
24/09/70.h
Normal file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/9/7.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_CPP_70_H
|
||||
#define LEECODE_CPP_70_H
|
||||
|
||||
//爬楼梯
|
||||
int climbStairs(int n) {
|
||||
|
||||
}
|
||||
|
||||
#endif //LEECODE_CPP_70_H
|
||||
26
24/11/3226.cpp
Normal file
26
24/11/3226.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
int minChanges(int n, int k)
|
||||
{
|
||||
if(n<k){
|
||||
return 1;
|
||||
}
|
||||
int count = 0;
|
||||
while (n || k == 0)
|
||||
{
|
||||
if ((n & 1) < (k & 1))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if ((n & 1) > (k & 1))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
n >>= 1;
|
||||
k >>= 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int minChanges1(int n, int k) {
|
||||
return (n & k) == k ? __builtin_popcount(n ^ k) : -1;
|
||||
}
|
||||
27
24/11/3243.cpp
Normal file
27
24/11/3243.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>> &queries)
|
||||
{
|
||||
vector<vector<int>> prev(n);
|
||||
vector<int> dp(n);
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
prev[i].push_back(i - 1);
|
||||
dp[i] = i;
|
||||
}
|
||||
vector<int> res;
|
||||
for (auto &query : queries)
|
||||
{
|
||||
prev[query[1]].push_back(query[0]);
|
||||
for (int v = query[1]; v < n; v++)
|
||||
{
|
||||
for (int u : prev[v])
|
||||
{
|
||||
dp[v] = min(dp[v], dp[u] + 1);
|
||||
}
|
||||
}
|
||||
res.push_back(dp[n - 1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
36
24/11/3258.cpp
Normal file
36
24/11/3258.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int countKConstraintSubstrings(string s, int k)
|
||||
{
|
||||
int a, b;
|
||||
int count = 0;
|
||||
for (int i = 1; i <= s.length(); i++)
|
||||
{
|
||||
int j = 0;
|
||||
while (j + i <= s.length())
|
||||
{
|
||||
a = 0;
|
||||
b = 0;
|
||||
for (int k = j; k < j + i; k++)
|
||||
{
|
||||
if (s[k] == '0')
|
||||
{
|
||||
a++;
|
||||
}
|
||||
else
|
||||
{
|
||||
b++;
|
||||
}
|
||||
}
|
||||
if (a <= k || b <= k)
|
||||
{
|
||||
count++;
|
||||
cout << j << " " << j + i << " " << a << " " << b << endl;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
51
24/11/3259.cpp
Normal file
51
24/11/3259.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "math.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 3259. 能量提升
|
||||
// 动态规划+贪心 递推式:dp[i] += max(now[i-1], next[i-2])
|
||||
long long maxEnergyBoost(vector<int> &energyDrinkA, vector<int> &energyDrinkB)
|
||||
{
|
||||
int n = energyDrinkA.size();
|
||||
vector<vector<long long>> dp(n + 1, vector<long long>(2, 0));
|
||||
dp[1][0] = energyDrinkA[0];
|
||||
dp[1][1] = energyDrinkB[0];
|
||||
for (int i = 2; i <= n; i++)
|
||||
{
|
||||
dp[i][0] = energyDrinkA[i - 1] + max(dp[i - 1][0], dp[i - 2][1]);
|
||||
dp[i][1] = energyDrinkB[i - 1] + max(dp[i - 1][1], dp[i - 2][0]);
|
||||
}
|
||||
return max(dp[n][0], dp[n][1]);
|
||||
}
|
||||
|
||||
long long maxEnergyBoost(int* energyDrinkA, int energyDrinkASize, int* energyDrinkB, int energyDrinkBSize) {
|
||||
int n = energyDrinkASize;
|
||||
long long **dp = (long long **)malloc(sizeof(long long *) * 2);
|
||||
dp[0] = (long long *)malloc(sizeof(long long) * (n + 1));
|
||||
dp[1] = (long long *)malloc(sizeof(long long) * (n + 1));
|
||||
dp[0][0] = 0;
|
||||
dp[1][0] = 0;
|
||||
dp[0][1] = energyDrinkA[0];
|
||||
dp[1][1] = energyDrinkB[0];
|
||||
for (int i = 2; i <= n; i++)
|
||||
{
|
||||
dp[0][i] = energyDrinkA[i-1] + fmax(dp[0][i-1], dp[1][i-2]);
|
||||
dp[1][i] = energyDrinkB[i-1] + fmax(dp[1][i-1], dp[0][i-2]);
|
||||
}
|
||||
return fmax(dp[0][n], dp[1][n]);
|
||||
}
|
||||
|
||||
|
||||
// 空间优化
|
||||
long long maxEnergyBoost1(int* energyDrinkA, int energyDrinkASize, int* energyDrinkB, int energyDrinkBSize) {
|
||||
int n = energyDrinkASize;
|
||||
long long tmp[3]={energyDrinkA[0],energyDrinkB[0],energyDrinkA[0]};
|
||||
for (int i = 1; i < energyDrinkASize; i++) {
|
||||
tmp[0]=fmax(tmp[0]+energyDrinkA[i],tmp[1]);
|
||||
tmp[1]=fmax(tmp[1]+energyDrinkB[i],tmp[2]);
|
||||
tmp[2]=tmp[0];
|
||||
}
|
||||
return fmax(tmp[0],tmp[1]);
|
||||
}
|
||||
108
24/11/Llist.c
Normal file
108
24/11/Llist.c
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ListNode
|
||||
{
|
||||
int val;
|
||||
ListNode *next;
|
||||
} ListNode;
|
||||
|
||||
|
||||
// 若使用了带头节点的链表,则无需判断head是否为空
|
||||
ListNode *createListNode(int val)
|
||||
{
|
||||
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
void printList(ListNode *head)
|
||||
{
|
||||
ListNode *temp = head;
|
||||
while(temp){
|
||||
printf("%d ",temp->val);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// 添加节点
|
||||
ListNode *addNode(ListNode *head,int val)
|
||||
{
|
||||
if(head == NULL){
|
||||
head = createListNode(val);
|
||||
return head;
|
||||
}
|
||||
ListNode *temp = head;
|
||||
while(temp->next){
|
||||
temp = temp->next;
|
||||
}
|
||||
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
temp->next = node;
|
||||
return head;
|
||||
}
|
||||
|
||||
int Locate(ListNode *head,int val){
|
||||
if(head == NULL){
|
||||
return -1;
|
||||
}
|
||||
ListNode *temp = head;
|
||||
int index = 0;
|
||||
while(temp){
|
||||
if(temp->val==val){
|
||||
return index;
|
||||
}
|
||||
temp = temp->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 删除节点
|
||||
ListNode *deleteNode(ListNode *head,int val){
|
||||
if(head == NULL){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ListNode *temp1 = head;
|
||||
if(temp1->val == val){
|
||||
head = temp1->next;
|
||||
free(temp1);
|
||||
return head;
|
||||
}
|
||||
|
||||
ListNode *temp2 = head->next;
|
||||
while (temp2)
|
||||
{
|
||||
if(temp2->val == val){
|
||||
temp1->next = temp2->next;
|
||||
free(temp2);
|
||||
return head;
|
||||
}
|
||||
temp1 = temp1->next;
|
||||
temp2 = temp2->next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
ListNode *insertNode(ListNode *head,int val,int index){
|
||||
if(head == NULL){
|
||||
head = createListNode(val);
|
||||
return head;
|
||||
}
|
||||
|
||||
ListNode *temp = head;
|
||||
int i = 0;
|
||||
while(i < index-1 && temp){
|
||||
temp = temp->next;
|
||||
i++;
|
||||
}
|
||||
|
||||
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = temp->next;
|
||||
temp->next = node;
|
||||
return head;
|
||||
}
|
||||
66
24/11/euclidean.c
Normal file
66
24/11/euclidean.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MaxSize 100
|
||||
|
||||
struct stacks{
|
||||
int data[MaxSize];
|
||||
int top;
|
||||
};
|
||||
|
||||
char *euclidean(int n, int r){
|
||||
struct stacks s;
|
||||
s.top = -1;
|
||||
|
||||
// 检查基数范围
|
||||
if (r < 2 || r > 36) {
|
||||
return NULL; // 无效的基数
|
||||
}
|
||||
|
||||
// 处理 n 为 0 的情况
|
||||
if (n == 0) {
|
||||
char *result = (char *)malloc(2);
|
||||
if (!result) return NULL; // 内存分配失败
|
||||
result[0] = '0';
|
||||
result[1] = '\0';
|
||||
return result;
|
||||
}
|
||||
|
||||
// 处理负数
|
||||
int isNegative = 0;
|
||||
if (n < 0) {
|
||||
isNegative = 1;
|
||||
n = -n;
|
||||
}
|
||||
|
||||
while(n > 0){
|
||||
if (s.top + 1 >= MaxSize) {
|
||||
return NULL; // 栈溢出
|
||||
}
|
||||
s.data[++s.top] = n % r;
|
||||
n = n / r;
|
||||
}
|
||||
|
||||
// 计算需要的内存大小:字符数 + 可能的负号 + 终止符
|
||||
int size = s.top + 1 + isNegative + 1;
|
||||
char *result = (char *)malloc(sizeof(char) * size);
|
||||
if (!result) {
|
||||
return NULL; // 内存分配失败
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
if (isNegative) {
|
||||
result[i++] = '-';
|
||||
}
|
||||
|
||||
while(s.top != -1){
|
||||
int value = s.data[s.top--];
|
||||
if (value < 10) {
|
||||
result[i++] = '0' + value;
|
||||
} else {
|
||||
result[i++] = 'A' + (value - 10);
|
||||
}
|
||||
}
|
||||
result[i] = '\0';
|
||||
return result;
|
||||
}
|
||||
38
24/11/length.cpp
Normal file
38
24/11/length.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "tree.c"
|
||||
|
||||
TreeNode* findLCA(TreeNode* root, int node1, int node2) {
|
||||
if (root == NULL || root->val == node1 || root->val == node2) {
|
||||
return root;
|
||||
}
|
||||
|
||||
TreeNode* left = findLCA(root->left, node1, node2);
|
||||
TreeNode* right = findLCA(root->right, node1, node2);
|
||||
|
||||
if (left != NULL && right != NULL) {
|
||||
return root;
|
||||
}
|
||||
|
||||
return (left != NULL) ? left : right;
|
||||
}
|
||||
|
||||
int findLevel(TreeNode* root, int val, int level) {
|
||||
if (root == NULL) return -1;
|
||||
if (root->val == val) return level;
|
||||
|
||||
int left = findLevel(root->left, val, level + 1);
|
||||
if (left != -1) return left;
|
||||
|
||||
return findLevel(root->right, val, level + 1);
|
||||
}
|
||||
|
||||
int nodeDistance(TreeNode *root, int node1, int node2) {
|
||||
TreeNode* lca = findLCA(root, node1, node2);
|
||||
if (lca == NULL) return -1;
|
||||
|
||||
int d1 = findLevel(lca, node1, 0);
|
||||
int d2 = findLevel(lca, node2, 0);
|
||||
|
||||
if (d1 == -1 || d2 == -1) return -1;
|
||||
|
||||
return d1 + d2;
|
||||
}
|
||||
219
24/11/tree.c
Normal file
219
24/11/tree.c
Normal file
@@ -0,0 +1,219 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef struct TreeNode
|
||||
{
|
||||
int val;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
typedef struct StackNode
|
||||
{
|
||||
TreeNode *treeNode;
|
||||
struct StackNode *next;
|
||||
} StackNode;
|
||||
|
||||
typedef struct Stack
|
||||
{
|
||||
StackNode *top;
|
||||
} Stack;
|
||||
|
||||
void push(Stack *stack, TreeNode *treeNode)
|
||||
{
|
||||
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
|
||||
newNode->treeNode = treeNode;
|
||||
newNode->next = stack->top;
|
||||
stack->top = newNode;
|
||||
}
|
||||
|
||||
TreeNode *pop(Stack *stack)
|
||||
{
|
||||
if (stack->top == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
StackNode *temp = stack->top;
|
||||
TreeNode *treeNode = temp->treeNode;
|
||||
stack->top = stack->top->next;
|
||||
free(temp);
|
||||
return treeNode;
|
||||
}
|
||||
|
||||
int isEmpty(Stack *stack)
|
||||
{
|
||||
return stack->top == NULL;
|
||||
}
|
||||
|
||||
void preorder(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack stack = {NULL};
|
||||
TreeNode *p = root;
|
||||
while (p != NULL || !isEmpty(&stack))
|
||||
{
|
||||
while (p != NULL)
|
||||
{
|
||||
printf("%d ", p->val);
|
||||
push(&stack, p);
|
||||
p = p->left;
|
||||
}
|
||||
p = pop(&stack);
|
||||
p = p->right;
|
||||
}
|
||||
}
|
||||
|
||||
void inorder(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack stack = {NULL};
|
||||
TreeNode *p = root;
|
||||
while (p != NULL || !isEmpty(&stack))
|
||||
{
|
||||
while (p != NULL)
|
||||
{
|
||||
push(&stack, p);
|
||||
p = p->left;
|
||||
}
|
||||
p = pop(&stack);
|
||||
printf("%d ", p->val);
|
||||
p = p->right;
|
||||
}
|
||||
}
|
||||
|
||||
void postorder(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack stack = {NULL};
|
||||
TreeNode *p = root;
|
||||
TreeNode *pre = NULL;
|
||||
do
|
||||
{
|
||||
while (p != NULL)
|
||||
{
|
||||
push(&stack, p);
|
||||
p = p->left;
|
||||
}
|
||||
p = pop(&stack);
|
||||
if (p->right == NULL || p->right == pre)
|
||||
{
|
||||
printf("%d ", p->val);
|
||||
pre = p;
|
||||
p = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
push(&stack, p);
|
||||
p = p->right;
|
||||
}
|
||||
} while (!isEmpty(&stack));
|
||||
}
|
||||
|
||||
void Postorderd(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Postorderd(root->left);
|
||||
Postorderd(root->right);
|
||||
printf("%d ", root->val);
|
||||
}
|
||||
|
||||
|
||||
TreeNode* createTreeNode(int val) {
|
||||
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
TreeNode* generateCompleteBinaryTree(int nodeCount) {
|
||||
if (nodeCount <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
TreeNode** nodes = (TreeNode**)malloc(nodeCount * sizeof(TreeNode*));
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
nodes[i] = createTreeNode(rand() % 100 + 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
if (2 * i + 1 < nodeCount) {
|
||||
nodes[i]->left = nodes[2 * i + 1];
|
||||
}
|
||||
if (2 * i + 2 < nodeCount) {
|
||||
nodes[i]->right = nodes[2 * i + 2];
|
||||
}
|
||||
}
|
||||
|
||||
TreeNode* root = nodes[0];
|
||||
free(nodes);
|
||||
return root;
|
||||
}
|
||||
|
||||
void printTreeStructure(TreeNode* root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
printf("%d ", root->val);
|
||||
if (root->left != NULL || root->right != NULL) {
|
||||
printf("(");
|
||||
printTreeStructure(root->left);
|
||||
printf(", ");
|
||||
printTreeStructure(root->right);
|
||||
printf(")");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
生成的二叉树结构如下:
|
||||
|
||||
2
|
||||
/ \
|
||||
/ \
|
||||
4 7
|
||||
/ \ / \
|
||||
9 11 22 44
|
||||
/ \ /
|
||||
66 88 99
|
||||
|
||||
*/
|
||||
TreeNode* generateSpecificBinaryTree()
|
||||
{
|
||||
int values[] = {2, 4, 7, 9, 11, 22, 44, 66, 88, 99};
|
||||
int nodeCount = sizeof(values) / sizeof(values[0]);
|
||||
|
||||
TreeNode** nodes = (TreeNode**)malloc(nodeCount * sizeof(TreeNode*));
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
nodes[i] = createTreeNode(values[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
if (2 * i + 1 < nodeCount) {
|
||||
nodes[i]->left = nodes[2 * i + 1];
|
||||
}
|
||||
if (2 * i + 2 < nodeCount) {
|
||||
nodes[i]->right = nodes[2 * i + 2];
|
||||
}
|
||||
}
|
||||
|
||||
TreeNode* root = nodes[0];
|
||||
free(nodes);
|
||||
return root;
|
||||
}
|
||||
18
24/12/3001.cpp
Normal file
18
24/12/3001.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
|
||||
// 车与皇后处在同一行,且中间没有象
|
||||
if (a == e && (c != a || d <= min(b, f) || d >= max(b, f))) {
|
||||
return 1;
|
||||
}
|
||||
// 车与皇后处在同一列,且中间没有象
|
||||
if (b == f && (d != b || c <= min(a, e) || c >= max(a, e))) {
|
||||
return 1;
|
||||
}
|
||||
// 象、皇后处在同一条对角线,且中间没有车
|
||||
if (abs(c - e) == abs(d - f) && ((c - e) * (b - f) != (a - e) * (d - f)
|
||||
|| a < min(c, e) || a > max(c, e))) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
40
25/05/2131.go
Normal file
40
25/05/2131.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
func Reverse(s string) string {
|
||||
bytes := []byte(s) // 直接转为字节切片
|
||||
for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
|
||||
bytes[i], bytes[j] = bytes[j], bytes[i] // 交换字节
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func longestPalindrome(words []string) int {
|
||||
m := make(map[string]int)
|
||||
res, center := 0, 0
|
||||
for _, word := range words {
|
||||
m[word]++
|
||||
}
|
||||
for word := range m {
|
||||
if m[word] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
reversed := Reverse(word)
|
||||
|
||||
if reversed == word {
|
||||
pairs := m[word] / 2
|
||||
res += pairs * 2 * 2
|
||||
if m[word]%2 == 1 {
|
||||
center = 2
|
||||
}
|
||||
} else {
|
||||
if m[reversed] >= 1 {
|
||||
pairs := min(m[word], m[reversed])
|
||||
res += pairs * 4
|
||||
m[word] -= pairs
|
||||
m[reversed] -= pairs
|
||||
}
|
||||
}
|
||||
}
|
||||
return res + center
|
||||
}
|
||||
9
25/05/2894.go
Normal file
9
25/05/2894.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
func differenceOfSums(n int, m int) int {
|
||||
nums2 := 0
|
||||
for i := 0; i*m <= n; i++ {
|
||||
nums2 += i * m
|
||||
}
|
||||
return n*(n+1)/2 - nums2*2
|
||||
}
|
||||
13
25/05/2942.go
Normal file
13
25/05/2942.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
func findWordsContaining(words []string, x byte) []int {
|
||||
var result []int
|
||||
for i, word := range words {
|
||||
if strings.Index(word, string(x)) != -1 {
|
||||
result = append(result, i)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
45
25/06/1061.go
Normal file
45
25/06/1061.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
type UnionFind struct {
|
||||
parent []int
|
||||
}
|
||||
|
||||
func NewUnionFind(n int) *UnionFind {
|
||||
uf := &UnionFind{parent: make([]int, n)}
|
||||
for i := 0; i < n; i++ {
|
||||
uf.parent[i] = i
|
||||
}
|
||||
return uf
|
||||
}
|
||||
|
||||
func (uf *UnionFind) Find(x int) int {
|
||||
if uf.parent[x] != x {
|
||||
uf.parent[x] = uf.Find(uf.parent[x])
|
||||
}
|
||||
return uf.parent[x]
|
||||
}
|
||||
|
||||
func (uf *UnionFind) Unite(x, y int) {
|
||||
x, y = uf.Find(x), uf.Find(y)
|
||||
if x == y {
|
||||
return
|
||||
}
|
||||
if x > y {
|
||||
x, y = y, x
|
||||
}
|
||||
// 总是让字典序更小的作为集合代表字符
|
||||
uf.parent[y] = x
|
||||
}
|
||||
|
||||
func smallestEquivalentString(s1 string, s2 string, baseStr string) string {
|
||||
uf := NewUnionFind(26)
|
||||
for i := 0; i < len(s1); i++ {
|
||||
uf.Unite(int(s1[i]-'a'), int(s2[i]-'a'))
|
||||
}
|
||||
|
||||
res := []byte(baseStr)
|
||||
for i := range res {
|
||||
res[i] = byte('a' + uf.Find(int(res[i]-'a')))
|
||||
}
|
||||
return string(res)
|
||||
}
|
||||
38
25/06/1432.go
Normal file
38
25/06/1432.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func maxDiff(num int) int {
|
||||
replace := func(s string, x, y rune) string {
|
||||
return strings.ReplaceAll(s, string(x), string(y))
|
||||
}
|
||||
|
||||
num_max := strconv.Itoa(num)
|
||||
num_min := num_max
|
||||
|
||||
for _, c := range num_max {
|
||||
if c != '9' {
|
||||
num_max = replace(num_max, c, '9')
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range num_min {
|
||||
if i == 0 && c != '1' {
|
||||
num_min = replace(num_min, c, '1')
|
||||
break
|
||||
}
|
||||
if c != '0' && c != rune(num_min[0]) {
|
||||
num_min = replace(num_min, c, '0')
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
x, _ := strconv.Atoi(num_max)
|
||||
y, _ := strconv.Atoi(num_min)
|
||||
|
||||
return x - y
|
||||
}
|
||||
13
25/06/2929.go
Normal file
13
25/06/2929.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
func distributeCandies(n int, limit int) int64 {
|
||||
var res int64 = 0
|
||||
for i := 0; i <= min(n, limit); i++ {
|
||||
last := n - i
|
||||
if last > 2*limit {
|
||||
continue
|
||||
}
|
||||
res += int64(min(last, limit) - max(0, last-limit) + 1)
|
||||
}
|
||||
return res
|
||||
}
|
||||
12
25/06/3423.go
Normal file
12
25/06/3423.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "math"
|
||||
|
||||
func maxAdjacentDistance(nums []int) int {
|
||||
n := len(nums)
|
||||
max := int(math.Abs(float64(nums[0] - nums[n-1])))
|
||||
for i := 0; i < n-1; i++ {
|
||||
max = int(math.Max(float64(max), float64(math.Abs(float64(nums[i]-nums[i+1])))))
|
||||
}
|
||||
return max
|
||||
}
|
||||
19
25/06/3442.go
Normal file
19
25/06/3442.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
func maxDifference(s string) int {
|
||||
m := make(map[rune]int)
|
||||
|
||||
maxOdd := 0
|
||||
minEven := len(s)
|
||||
for _, c := range s {
|
||||
m[c]++
|
||||
}
|
||||
for _, v := range m {
|
||||
if v%2 == 1 {
|
||||
maxOdd = max(maxOdd, v)
|
||||
} else {
|
||||
minEven = min(minEven, v)
|
||||
}
|
||||
}
|
||||
return maxOdd - minEven
|
||||
}
|
||||
18
25/06/386.go
Normal file
18
25/06/386.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
func lexicalOrder(n int) []int {
|
||||
ans := make([]int, n)
|
||||
num := 1
|
||||
for i := 0; i < n; i++ {
|
||||
ans[i] = num
|
||||
if num*10 <= n {
|
||||
num *= 10
|
||||
} else {
|
||||
for num%10 == 9 || num+1 > n {
|
||||
num /= 10
|
||||
}
|
||||
num++
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
15
README.md
Normal file
15
README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
- 02/2506
|
||||
- ```c++
|
||||
for (const string &word : words)
|
||||
{
|
||||
int state = 0;
|
||||
for (char c : word)
|
||||
{
|
||||
state |= 1 << (c - 'a');
|
||||
}
|
||||
res += cnt[state];
|
||||
cnt[state]++;
|
||||
}
|
||||
```
|
||||
- 这是一种位压缩算法,巧妙的利用二进制的位来表示一个单词中出现的所有字母。
|
||||
- 关键就是`state |= 1 << (c - 'a');`这一行。这一行的作用是将state的第(c - 'a')位设为1,这样就可以表示这个单词中出现了字母c。
|
||||
35
dataStruct/Combination.h
Normal file
35
dataStruct/Combination.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/19.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_COMBINATION_H
|
||||
#define LEECODE_C_COMBINATION_H
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
// 计算组合数 C(n, m)
|
||||
int calculateCnm(int n, int m) {
|
||||
if (m < 0 || m > n) {
|
||||
return 0;
|
||||
}
|
||||
if (m > n / 2) {
|
||||
m = n - m; // 优化,使用较小的 m 提高效率
|
||||
}
|
||||
|
||||
std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, 0));
|
||||
|
||||
for (int i = 0; i <= n; i++) {
|
||||
for (int j = 0; j <= std::min(i, m); j++) {
|
||||
if (j == 0 || j == i) {
|
||||
dp[i][j] = 1;
|
||||
} else {
|
||||
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[n][m];
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_COMBINATION_H
|
||||
23
dataStruct/Heap/Heap.h
Normal file
23
dataStruct/Heap/Heap.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/12.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_HEAP_H
|
||||
#define LEECODE_C_HEAP_H
|
||||
|
||||
#include "vector"
|
||||
template<typename T>
|
||||
class Heap {
|
||||
private:
|
||||
std::vector<T> array;
|
||||
public:
|
||||
int length;
|
||||
|
||||
Heap(std::vector<T> &array) : array(array){
|
||||
length = array.size();
|
||||
}
|
||||
|
||||
typedef void (Heap::*compare(T a,T b))(bool);
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_HEAP_H
|
||||
66
dataStruct/LinkedList/lists.h
Normal file
66
dataStruct/LinkedList/lists.h
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/6.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_LISTS_H
|
||||
#define LEECODE_C_LISTS_H
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
|
||||
void list() {
|
||||
ListNode *temp = this;
|
||||
while (temp) {
|
||||
std::cout << temp->val << " ";
|
||||
temp = temp->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int *array() {
|
||||
int *array = new int[len()];
|
||||
ListNode *temp = this;
|
||||
int index = 0;
|
||||
while (temp) {
|
||||
array[index++] = temp->val;
|
||||
temp = temp->next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
int len() {
|
||||
int length = 0;
|
||||
ListNode *temp = this;
|
||||
while (temp) {
|
||||
temp = temp->next;
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
};
|
||||
|
||||
ListNode *createRandomList(int len) {
|
||||
ListNode *p = nullptr;
|
||||
std::random_device rd;
|
||||
std::uniform_int_distribution<int> dis(1, 100);
|
||||
std::mt19937 gen(rd());
|
||||
while (--len >= 0) {
|
||||
auto *temp = new ListNode(dis(gen), p);
|
||||
p = temp;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
#endif //LEECODE_C_LISTS_H
|
||||
128
dataStruct/Queue/PriorityQueue.c
Normal file
128
dataStruct/Queue/PriorityQueue.c
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_SIZE 100
|
||||
|
||||
typedef enum { INT_TYPE, FLOAT_TYPE, DOUBLE_TYPE } ElementType;
|
||||
|
||||
typedef struct {
|
||||
ElementType type;
|
||||
union {
|
||||
int intValue;
|
||||
float floatValue;
|
||||
double doubleValue;
|
||||
} data;
|
||||
} Item;
|
||||
|
||||
typedef struct {
|
||||
Item items[MAX_SIZE];
|
||||
int size;
|
||||
} PriorityQueue;
|
||||
|
||||
PriorityQueue createPriorityQueue() {
|
||||
PriorityQueue pq;
|
||||
pq.size = 0;
|
||||
return pq;
|
||||
}
|
||||
|
||||
bool isLess(Item item1, Item item2) {
|
||||
if (item1.type != item2.type) {
|
||||
fprintf(stderr, "错误:无法比较不同类型的元素。\n");
|
||||
return false;
|
||||
}
|
||||
switch (item1.type) {
|
||||
case INT_TYPE:
|
||||
return item1.data.intValue < item2.data.intValue;
|
||||
case FLOAT_TYPE:
|
||||
return item1.data.floatValue < item2.data.floatValue;
|
||||
case DOUBLE_TYPE:
|
||||
return item1.data.doubleValue < item2.data.doubleValue;
|
||||
default:
|
||||
fprintf(stderr, "错误:未知的元素类型。\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void swap(Item* item1, Item* item2) {
|
||||
Item temp = *item1;
|
||||
*item1 = *item2;
|
||||
*item2 = temp;
|
||||
}
|
||||
|
||||
void push(PriorityQueue* pq, Item newItem) {
|
||||
if (pq->size >= MAX_SIZE) {
|
||||
fprintf(stderr, "错误:优先队列已满,无法插入元素。\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int childIndex = pq->size;
|
||||
pq->items[childIndex] = newItem;
|
||||
pq->size++;
|
||||
|
||||
int parentIndex = (childIndex - 1) / 2;
|
||||
while (childIndex > 0 && isLess(pq->items[childIndex], pq->items[parentIndex])) {
|
||||
swap(&pq->items[childIndex], &pq->items[parentIndex]);
|
||||
childIndex = parentIndex;
|
||||
parentIndex = (childIndex - 1) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
Item pop(PriorityQueue* pq) {
|
||||
if (pq->size <= 0) {
|
||||
fprintf(stderr, "错误:优先队列为空,无法弹出元素。\n");
|
||||
Item emptyItem;
|
||||
emptyItem.type = INT_TYPE; // 返回一个空元素
|
||||
emptyItem.data.intValue = 0;
|
||||
return emptyItem;
|
||||
}
|
||||
|
||||
Item minItem = pq->items[0];
|
||||
pq->size--;
|
||||
|
||||
pq->items[0] = pq->items[pq->size];
|
||||
int parentIndex = 0;
|
||||
while (true) {
|
||||
int leftChildIndex = parentIndex * 2 + 1;
|
||||
int rightChildIndex = parentIndex * 2 + 2;
|
||||
int smallestIndex = parentIndex;
|
||||
|
||||
if (leftChildIndex < pq->size && isLess(pq->items[leftChildIndex], pq->items[smallestIndex])) {
|
||||
smallestIndex = leftChildIndex;
|
||||
}
|
||||
if (rightChildIndex < pq->size && isLess(pq->items[rightChildIndex], pq->items[smallestIndex])) {
|
||||
smallestIndex = rightChildIndex;
|
||||
}
|
||||
|
||||
if (smallestIndex == parentIndex) {
|
||||
break; // 已经是最小堆,无需继续下沉
|
||||
}
|
||||
|
||||
swap(&pq->items[parentIndex], &pq->items[smallestIndex]);
|
||||
parentIndex = smallestIndex;
|
||||
}
|
||||
|
||||
return minItem;
|
||||
}
|
||||
|
||||
int main() {
|
||||
PriorityQueue pq = createPriorityQueue();
|
||||
|
||||
// 向优先队列中插入一些元素
|
||||
Item item1, item2, item3;
|
||||
item1.type = INT_TYPE;
|
||||
item1.data.intValue = 5;
|
||||
item2.type = INT_TYPE;
|
||||
item2.data.intValue = 2;
|
||||
item3.type = INT_TYPE;
|
||||
item3.data.intValue = 10;
|
||||
|
||||
push(&pq, item1);
|
||||
push(&pq, item2);
|
||||
push(&pq, item3);
|
||||
|
||||
// 从优先队列中弹出元素
|
||||
Item minItem = pop(&pq);
|
||||
printf("弹出的最小元素为:%d\n", minItem.data.intValue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
dataStruct/Queue/README.md
Normal file
15
dataStruct/Queue/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# QUEUE
|
||||
|
||||
## PriorityQueue - 优先队列
|
||||
|
||||
- 优先队列也是一种队列,只不过不同的是,优先队列的出队顺序是按照优先级来的,可能需要找到元素集合中的最小或者最大元素,可以利用优先队列ADT来完成操作,优先队列ADT是一种数据结构,它支持插入和删除最小值操作(返回并删除最小元素)或删除最大值操作(返回并删除最大元素);
|
||||
|
||||
- 这些操作等价于队列的`enQueue`和`deQueue`操作,区别在于,对于优先队列,元素进入队列的顺序可能与其被操作的顺序不同,作业调度是优先队列的一个应用实例,它根据优先级的高低而不是先到先服务的方式来进行调度;
|
||||
- 如果最小键值元素拥有最高的优先级,那么这种优先队列叫作**升序优先队列**(即总是先删除最小的元素),类似的,如果最大键值元素拥有最高的优先级,那么这种优先队列叫作**降序优先队列**(即总是先删除最大的元素);由于这两种类型时对称的,所以只需要关注其中一种,如升序优先队列;
|
||||
- 优先队列的应用
|
||||
- 数据压缩:赫夫曼编码算法;
|
||||
- 最短路径算法:Dijkstra算法;
|
||||
- 最小生成树算法:Prim算法;
|
||||
- 事件驱动仿真:顾客排队算法;
|
||||
- 选择问题:查找第k个最小元素;
|
||||
- 等等等等....
|
||||
66
dataStruct/Tree/Tree.h
Normal file
66
dataStruct/Tree/Tree.h
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/14.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_TREE_H
|
||||
#define LEECODE_C_TREE_H
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <queue>
|
||||
|
||||
TreeNode *creatRandomTree(int size) {
|
||||
if (size == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<int> dis(1, 100);
|
||||
|
||||
std::queue<int> Q;
|
||||
std::queue<TreeNode *> T;
|
||||
for (int i = size; i > 0; --i) {
|
||||
Q.push(dis(gen));
|
||||
std::cout << Q.back() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
TreeNode *head = new TreeNode(Q.front());
|
||||
T.push(head);
|
||||
Q.pop();
|
||||
|
||||
while (!Q.empty()) {
|
||||
if (T.front()->left != nullptr && T.front()->right != nullptr) {
|
||||
T.pop();
|
||||
}
|
||||
|
||||
TreeNode *temp = new TreeNode(Q.front());
|
||||
Q.pop();
|
||||
|
||||
if (T.front()->left == nullptr) {
|
||||
T.front()->left = temp;
|
||||
T.push(temp);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (T.front()->right == nullptr) {
|
||||
T.front()->right = temp;
|
||||
T.push(temp);
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_TREE_H
|
||||
56
dataStruct/Tree/TreeStack.h
Normal file
56
dataStruct/Tree/TreeStack.h
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/9/6.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_TREESTACK_H
|
||||
#define LEECODE_C_TREESTACK_H
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "./Tree.h"
|
||||
|
||||
class TreeStack {
|
||||
private:
|
||||
std::vector<TreeNode *> data;
|
||||
|
||||
public:
|
||||
// 入栈操作
|
||||
void push(TreeNode *value) {
|
||||
data.push_back(value);
|
||||
}
|
||||
|
||||
// 出栈操作
|
||||
TreeNode *pop() {
|
||||
if (!isEmpty()) {
|
||||
TreeNode *topValue = data.back();
|
||||
data.pop_back();
|
||||
return topValue;
|
||||
} else {
|
||||
std::cerr << "Error: Stack is empty." << std::endl;
|
||||
return nullptr; // 可以根据需要返回其他值或抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
// 获取栈顶元素
|
||||
TreeNode *top() const {
|
||||
if (!isEmpty()) {
|
||||
return data.back();
|
||||
} else {
|
||||
std::cerr << "Error: Stack is empty." << std::endl;
|
||||
return nullptr; // 可以根据需要返回其他值或抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
// 检查栈是否为空
|
||||
bool isEmpty() const {
|
||||
return data.empty();
|
||||
}
|
||||
|
||||
// 获取栈的大小
|
||||
size_t size() const {
|
||||
return data.size();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_TREESTACK_H
|
||||
23
dynamic planning/*22.cpp
Normal file
23
dynamic planning/*22.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> generateParenthesis(int n) {
|
||||
vector<string> v;
|
||||
dfs(v, "", n, n);
|
||||
return v;
|
||||
}
|
||||
void dfs(vector<string> &v, string s, int lc, int rc){
|
||||
if(lc == 0 && rc == 0){
|
||||
v.push_back(s);
|
||||
return;
|
||||
}
|
||||
if(lc != 0) dfs(v, s + '(', lc - 1, rc);
|
||||
if(rc != 0 && rc - 1 >= lc){
|
||||
dfs(v, s + ')', lc, rc - 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
29
else/DivConvert.h
Normal file
29
else/DivConvert.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/2.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_DIVCONVERT_H
|
||||
#define LEECODE_C_DIVCONVERT_H
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string decimalToBinaryUsingEuclidean(int target) {
|
||||
stack<int> s;
|
||||
string result = "";
|
||||
while (target) {
|
||||
s.push(target % 2);
|
||||
target /= 2;
|
||||
}
|
||||
while (!s.empty()) {
|
||||
result.append(to_string(s.top()));
|
||||
s.pop();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// test : cout << decimalToBinaryUsingEuclidean(555) << endl;
|
||||
|
||||
#endif //LEECODE_C_DIVCONVERT_H
|
||||
0
else/Heap/README.md
Normal file
0
else/Heap/README.md
Normal file
34
else/Heap/test.h
Normal file
34
else/Heap/test.h
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/18.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_TEST_H_P
|
||||
#define LEECODE_C_TEST_H_P
|
||||
|
||||
#include <queue>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void testPQ() {
|
||||
priority_queue<int, deque<int>, less<>> maxHeap;
|
||||
maxHeap.push(1);
|
||||
maxHeap.push(10);
|
||||
maxHeap.push(3);
|
||||
maxHeap.push(3);
|
||||
maxHeap.push(3);
|
||||
cout << maxHeap.top() << endl;
|
||||
maxHeap.pop();
|
||||
maxHeap.push(4);
|
||||
cout << maxHeap.top() << endl;
|
||||
maxHeap.pop();
|
||||
maxHeap.push(2);
|
||||
cout << maxHeap.top() << endl;
|
||||
priority_queue<int, vector<int>, greater<>> minHeap;
|
||||
minHeap.push(20);
|
||||
minHeap.push(10);
|
||||
minHeap.push(30);
|
||||
cout << minHeap.top() << endl;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_TEST_H_P
|
||||
23
else/README.md
Normal file
23
else/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# ELSE
|
||||
|
||||
-
|
||||
- [X] [用栈实现辗转相除法](./DivConvert.h) - P59
|
||||
-
|
||||
- [X] [对称序列](./symmetry.h)
|
||||
-
|
||||
- [X] [括号匹配](./kuohao.h) - P63
|
||||
-
|
||||
- [ ] 二叉树计算
|
||||
-
|
||||
- [X] [二叉树代码 递归 + 非递归前中后序遍历](./traverse.h)
|
||||
-
|
||||
- [X] [快速排序](./sorts.h)
|
||||
|
||||
- [ ] [堆排序](./sorts.h)
|
||||
-
|
||||
- [X] [图](./structs/Graph.h)
|
||||
|
||||
- [X] [深度优先遍历](./structs/Graph.h)
|
||||
|
||||
|
||||
|
||||
31
else/demo/1.h
Normal file
31
else/demo/1.h
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/8.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_1_H
|
||||
#define LEECODE_C_1_H
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void test1() {
|
||||
//方式一
|
||||
int a;
|
||||
cout << "输入长度:";
|
||||
cin >> a;
|
||||
int *p = (int *) malloc(a * sizeof(int)); //c写法在运行中分配空间创建指定长度数组
|
||||
p[a - 1] = 1;
|
||||
cout << p[a-1] << endl;
|
||||
|
||||
//方式2 推荐
|
||||
vector<int> b; //c++ 向量,可以看作是动态数组,自动扩充容量
|
||||
vector<vector<int>> c; //二维动态数组
|
||||
b.push_back(1);
|
||||
b.push_back(2);
|
||||
b.size(); //获取当前长度
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_1_H
|
||||
24
else/demo/2.h
Normal file
24
else/demo/2.h
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/11.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2_H
|
||||
#define LEECODE_C_2_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void test1(int *p, int index) {
|
||||
cout << p[index];
|
||||
}
|
||||
|
||||
void run() {
|
||||
int arr[3];
|
||||
arr[0] = 1;
|
||||
arr[1] = 2;
|
||||
arr[2] = 3;
|
||||
test1(arr, 2);
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_2_H
|
||||
52
else/kuohao.h
Normal file
52
else/kuohao.h
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/4.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_KUOHAO_H
|
||||
#define LEECODE_C_KUOHAO_H
|
||||
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
bool isKuoHao(std::string target) {
|
||||
std::stack<char> tk;
|
||||
for (int i = 0; i < target.length(); ++i) {
|
||||
if (tk.empty()) {
|
||||
tk.push(target[i]);
|
||||
continue;
|
||||
}
|
||||
char temp;
|
||||
switch (target[i]) {
|
||||
case '{':
|
||||
temp = '}';
|
||||
break;
|
||||
case '}':
|
||||
temp = '{';
|
||||
break;
|
||||
case '[':
|
||||
temp = ']';
|
||||
break;
|
||||
case ']':
|
||||
temp = '[';
|
||||
break;
|
||||
case '(':
|
||||
temp = ')';
|
||||
break;
|
||||
case ')':
|
||||
temp = '(';
|
||||
break;
|
||||
default:
|
||||
temp = 'x';
|
||||
}
|
||||
if (temp == tk.top()) {
|
||||
tk.pop();
|
||||
} else {
|
||||
tk.push(target[i]);
|
||||
}
|
||||
}
|
||||
return tk.empty();
|
||||
}
|
||||
|
||||
// cout << isKuoHao("{}(({}))") << endl;
|
||||
|
||||
#endif //LEECODE_C_KUOHAO_H
|
||||
31
else/main.h
Normal file
31
else/main.h
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/12.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_MAIN_H
|
||||
#define LEECODE_C_MAIN_H
|
||||
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include "./structs/Tree.h"
|
||||
#include "./traverse.h"
|
||||
#include "./Heap/test.h"
|
||||
#include "./sorts.h"
|
||||
#include "./structs/Graph.h"
|
||||
#include "./topic/2019.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void run()
|
||||
{
|
||||
vector<int> arr({3, 2, 2, 4, 6, 2, 3});
|
||||
odd_even(arr);
|
||||
for (int i : arr)
|
||||
{
|
||||
cout << i << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
#endif // LEECODE_C_MAIN_H
|
||||
171
else/sorts.h
Normal file
171
else/sorts.h
Normal file
@@ -0,0 +1,171 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/19.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_SHELLSORT_H
|
||||
#define LEECODE_C_SHELLSORT_H
|
||||
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void shellSort(vector<int> &arr) {
|
||||
int n = arr.size();
|
||||
|
||||
// 选择增量序列,可以根据需要选择不同的序列
|
||||
for (int gap = n / 2; gap > 0; gap /= 2) {
|
||||
// 使用插入排序对每个子数组进行排序
|
||||
for (int i = gap; i < n; ++i) {
|
||||
int temp = arr[i];
|
||||
int j = i;
|
||||
while (j >= gap && arr[j - gap] > temp) {
|
||||
arr[j] = arr[j - gap];
|
||||
j -= gap;
|
||||
}
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 快速排序 - start
|
||||
int partition(vector<int> &arr,int left,int right){
|
||||
int sign = arr[left];
|
||||
while(left < right){
|
||||
while(left < right && sign <= arr[right]){
|
||||
right--;
|
||||
}
|
||||
swap(arr[left],arr[right]);
|
||||
while(left < right && arr[left] <= sign){
|
||||
left++;
|
||||
}
|
||||
swap(arr[left],arr[right]);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
void splits(vector<int> &arr,int left,int right){
|
||||
if (left < right)
|
||||
{
|
||||
int norm = partition(arr,left,right);
|
||||
splits(arr,left,norm - 1);
|
||||
splits(arr,norm + 1,right);
|
||||
}
|
||||
}
|
||||
|
||||
void quickSort(vector<int> &arr) {
|
||||
splits(arr, 0, arr.size() - 1);
|
||||
}
|
||||
// 快速排序 - end
|
||||
|
||||
// 堆排序 - start small -> large
|
||||
void maxHeapify(vector<int> &arr, int n, int i) {
|
||||
int largest = i;
|
||||
int left = i * 2 + 1;
|
||||
int right = i * 2 + 2;
|
||||
if (left < n && arr[left] > arr[largest]) {
|
||||
largest = left;
|
||||
}
|
||||
if (right < n && arr[right] > arr[largest]) {
|
||||
largest = right;
|
||||
}
|
||||
if (largest != i) {
|
||||
swap(arr[i], arr[largest]);
|
||||
maxHeapify(arr, n, largest);
|
||||
}
|
||||
}
|
||||
|
||||
void heapSort(vector<int> &arr) {
|
||||
auto n = arr.size();
|
||||
for (int i = n / 2 - 1; i >= 0; --i) {
|
||||
maxHeapify(arr, n, i);
|
||||
}
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
swap(arr[0], arr[i]);
|
||||
maxHeapify(arr, i, 0);
|
||||
}
|
||||
}
|
||||
// 堆排序 - end small -> large
|
||||
|
||||
// 归并排序
|
||||
void merge(std::vector<int>& arr, int left, int middle, int right) {
|
||||
int n1 = middle - left + 1;
|
||||
int n2 = right - middle;
|
||||
|
||||
std::vector<int> leftArr(n1);
|
||||
std::vector<int> rightArr(n2);
|
||||
|
||||
for (int i = 0; i < n1; i++) {
|
||||
leftArr[i] = arr[left + i];
|
||||
}
|
||||
for (int i = 0; i < n2; i++) {
|
||||
rightArr[i] = arr[middle + 1 + i];
|
||||
}
|
||||
|
||||
int i = 0, j = 0, k = left;
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (leftArr[i] <= rightArr[j]) {
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = rightArr[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
while (i < n1) {
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
while (j < n2) {
|
||||
arr[k] = rightArr[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
void mergeSort(std::vector<int>& arr, int left, int right) {
|
||||
if (left < right) {
|
||||
int middle = left + (right - left) / 2;
|
||||
mergeSort(arr, left, middle);
|
||||
mergeSort(arr, middle + 1, right);
|
||||
merge(arr, left, middle, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void runS() {
|
||||
// std::random_device rd;
|
||||
// std::mt19937 gen(rd());
|
||||
// std::uniform_int_distribution<int> dis(1, 100);
|
||||
vector<int> nums;
|
||||
nums.reserve(10);
|
||||
// for (int i = 0; i < 10; ++i) {
|
||||
// nums.push_back(dis(gen));
|
||||
// }
|
||||
nums.push_back(11);
|
||||
nums.push_back(6);
|
||||
nums.push_back(9);
|
||||
nums.push_back(23);
|
||||
nums.push_back(6);
|
||||
nums.push_back(11);
|
||||
nums.push_back(26);
|
||||
nums.push_back(40);
|
||||
nums.push_back(8);
|
||||
nums.push_back(95);
|
||||
for (int i: nums) {
|
||||
cout << i << " ";
|
||||
}
|
||||
cout << endl;
|
||||
shellSort(nums);
|
||||
for (int i: nums) {
|
||||
cout << i << " ";
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_SHELLSORT_H
|
||||
181
else/structs/Graph.h
Normal file
181
else/structs/Graph.h
Normal file
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/21.
|
||||
// 以邻接矩阵实现的INT图及其具体操作
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_GRAPH_H
|
||||
#define LEECODE_C_GRAPH_H
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Graph {
|
||||
public:
|
||||
int size;
|
||||
|
||||
Graph(int n, bool direction) {
|
||||
this->size = n;
|
||||
G = vector<vector<int>>(size, vector<int>(size, 0));
|
||||
visited = vector<int>(size, 0);
|
||||
|
||||
random_device rd;
|
||||
mt19937 gen(rd());
|
||||
uniform_int_distribution<int> dis(0, 1);
|
||||
|
||||
if (!direction) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = i + 1; j < size; ++j) {
|
||||
if (dis(gen)) {
|
||||
G[i][j] = 1;
|
||||
G[j][i] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = 0; j < size; ++j) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
if (dis(gen)) {
|
||||
G[i][j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 0 1 0 0 0 0
|
||||
// 1 0 1 1 0 0
|
||||
// 0 1 0 0 0 1
|
||||
// 0 1 0 0 0 0
|
||||
// 0 0 0 0 0 1
|
||||
// 0 0 1 0 1 0
|
||||
Graph(int x) {
|
||||
size = 6;
|
||||
G = vector<vector<int>>(size, vector<int>(size, 0));
|
||||
visited = vector<int>(size, 0);
|
||||
vector<vector<int>> Edges;
|
||||
if (x == 1) {
|
||||
Edges.push_back({0, 1});
|
||||
Edges.push_back({1, 2});
|
||||
Edges.push_back({1, 3});
|
||||
Edges.push_back({2, 5});
|
||||
Edges.push_back({4, 5});
|
||||
}
|
||||
if (x == 2) {
|
||||
|
||||
}
|
||||
for (auto Edge: Edges) {
|
||||
G[Edge[0]][Edge[1]] = 1;
|
||||
G[Edge[1]][Edge[0]] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
Graph *GraphByEdge(vector<vector<int>> &Edges) {
|
||||
for (auto Edge: Edges) {
|
||||
vertex.insert(Edge[1]);
|
||||
vertex.insert(Edge[2]);
|
||||
}
|
||||
|
||||
size = vertex.size();
|
||||
G = vector<vector<int>>(size, vector<int>(size, 0));
|
||||
|
||||
for (auto Edge: Edges) {
|
||||
G[Edge[1]][Edge[2]] = 1;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
void printG() {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = 0; j < size; ++j) {
|
||||
cout << G[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
queue<int> DFS(int start) {
|
||||
queue<int> Q;
|
||||
stack<pair<int, int>> S;
|
||||
Q.push(start);
|
||||
S.emplace(start, -1);
|
||||
visited[0] = 1;
|
||||
while (!S.empty()) {
|
||||
auto temp = S.top();
|
||||
auto next = NextPoint(temp.first, temp.second);
|
||||
S.top().second = next;
|
||||
if (next == -1) {
|
||||
S.pop();
|
||||
continue;
|
||||
}
|
||||
if (visited[next]) {
|
||||
continue;
|
||||
}
|
||||
Q.push(next);
|
||||
visited[next] = 1;
|
||||
S.emplace(next, -1);
|
||||
}
|
||||
if (Q.size() != size) {
|
||||
// 将其他的点再进行DSF
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
|
||||
queue<int> BFS(int start) {
|
||||
queue<int> Q;
|
||||
queue<pair<int, int>> S;
|
||||
Q.push(start);
|
||||
S.emplace(start, -1);
|
||||
visited[0] = 1;
|
||||
while (!S.empty()) {
|
||||
auto temp = S.front();
|
||||
auto next = NextPoint(temp.first, temp.second);
|
||||
S.front().second = next;
|
||||
if (next == -1) {
|
||||
S.pop();
|
||||
continue;
|
||||
}
|
||||
if (visited[next]) {
|
||||
continue;
|
||||
}
|
||||
Q.push(next);
|
||||
visited[next] = 1;
|
||||
S.emplace(next, -1);
|
||||
}
|
||||
if (Q.size() != size) {
|
||||
// 将其他的点再进行BSF
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
|
||||
int NextPoint(int vex, int now) {
|
||||
auto row = G[vex];
|
||||
for (int i = now + 1; i < size; ++i) {
|
||||
if (row[i] != 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private:
|
||||
vector<vector<int>> G; // adjacent matrix
|
||||
vector<int> visited;
|
||||
|
||||
set<int> vertex; // The collection of vertex
|
||||
set<set<int>> ccp; // The graph has multiple connected components.
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_GRAPH_H
|
||||
8
else/structs/List.h
Normal file
8
else/structs/List.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef List_h
|
||||
#define List_h
|
||||
struct ListNode{
|
||||
int value;
|
||||
struct ListNode * next;
|
||||
}
|
||||
|
||||
#endif //List_h
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user