mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-01-12 18:08:38 +08:00
init
This commit is contained in:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user