commit 3f58f483ffd3051010990b22c569d5f92d4f098a Author: wolves Date: Mon Sep 15 21:12:04 2025 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8ecff1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +.idea + +.vscode + +.DS_Store + +**/.DS_Store + +.DS_Store? + +cmake-build-debug +CMakeFiles +build +cmake_install.cmake +CMakeCache.txt +Makefile +main \ No newline at end of file diff --git a/23/04/1019.c b/23/04/1019.c new file mode 100644 index 0000000..2f78296 --- /dev/null +++ b/23/04/1019.c @@ -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; +} \ No newline at end of file diff --git a/23/04/2399.c b/23/04/2399.c new file mode 100644 index 0000000..f6b49af --- /dev/null +++ b/23/04/2399.c @@ -0,0 +1,20 @@ +#include +#include + +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; +} \ No newline at end of file diff --git a/23/05/1373.c b/23/05/1373.c new file mode 100644 index 0000000..1b8cbfd --- /dev/null +++ b/23/05/1373.c @@ -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; +} \ No newline at end of file diff --git a/23/05/33.c b/23/05/33.c new file mode 100644 index 0000000..92fb205 --- /dev/null +++ b/23/05/33.c @@ -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; +} \ No newline at end of file diff --git a/23/07/2208.cpp b/23/07/2208.cpp new file mode 100644 index 0000000..04bb8db --- /dev/null +++ b/23/07/2208.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +using namespace std; + +class Solution { + public: + int halveArray(vector& nums) { + priority_queue 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; + } +}; \ No newline at end of file diff --git a/23/07/2500.cpp b/23/07/2500.cpp new file mode 100644 index 0000000..757ea6e --- /dev/null +++ b/23/07/2500.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +class Solution { +public: + int deleteGreatestValue(vector>& 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; + } +}; \ No newline at end of file diff --git a/23/07/771.c b/23/07/771.c new file mode 100644 index 0000000..d3907d9 --- /dev/null +++ b/23/07/771.c @@ -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; +} \ No newline at end of file diff --git a/23/08/1281.cpp b/23/08/1281.cpp new file mode 100644 index 0000000..c55bf8d --- /dev/null +++ b/23/08/1281.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/23/08/1749.cpp b/23/08/1749.cpp new file mode 100644 index 0000000..3560af0 --- /dev/null +++ b/23/08/1749.cpp @@ -0,0 +1,9 @@ +#include "vector" +using namespace std; + +class Q1749 { +public: + int maxAbsoluteSum(vector& nums) { + + } +}; \ No newline at end of file diff --git a/23/08/21.cpp b/23/08/21.cpp new file mode 100644 index 0000000..0e6a1a3 --- /dev/null +++ b/23/08/21.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/23/08/24.cpp b/23/08/24.cpp new file mode 100644 index 0000000..0cf3b6d --- /dev/null +++ b/23/08/24.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/23/08/Q1388.cpp b/23/08/Q1388.cpp new file mode 100644 index 0000000..b905aae --- /dev/null +++ b/23/08/Q1388.cpp @@ -0,0 +1,13 @@ +// +// Created by 李洋 on 2023/8/18. +// +#include + +using namespace std; + +class Q1388 { +public: + int maxSizeSlices(vector &slices) { + + } +}; \ No newline at end of file diff --git a/23/08/Q1572.cpp b/23/08/Q1572.cpp new file mode 100644 index 0000000..d2692cb --- /dev/null +++ b/23/08/Q1572.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +class Q1572 { +public: + int diagonalSum_1(vector> &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; + } +}; \ No newline at end of file diff --git a/23/08/Q2235.cpp b/23/08/Q2235.cpp new file mode 100644 index 0000000..298ee56 --- /dev/null +++ b/23/08/Q2235.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/23/08/Q23.cpp b/23/08/Q23.cpp new file mode 100644 index 0000000..5995a94 --- /dev/null +++ b/23/08/Q23.cpp @@ -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& lists) { + } +}; \ No newline at end of file diff --git a/23/08/Q2682.cpp b/23/08/Q2682.cpp new file mode 100644 index 0000000..b847b2f --- /dev/null +++ b/23/08/Q2682.cpp @@ -0,0 +1,22 @@ +// +// Created by 李洋 on 2023/8/16. +// +#include "vector" +using namespace std; +class Q2682 { +public: + vector circularGameLosers(int n, int k) { + vector visit(n, false); + for (int i = k, j = 0; !visit[j]; i += k) { + visit[j] = true; + j = (j + i) % n; + } + vector ans; + for (int i = 0; i < n; i++) { + if (!visit[i]) { + ans.emplace_back(i + 1); + } + } + return ans; + } +}; \ No newline at end of file diff --git a/23/08/Q617.cpp b/23/08/Q617.cpp new file mode 100644 index 0000000..bcb17e3 --- /dev/null +++ b/23/08/Q617.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/23/08/Q88.cpp b/23/08/Q88.cpp new file mode 100644 index 0000000..028b486 --- /dev/null +++ b/23/08/Q88.cpp @@ -0,0 +1,28 @@ +// +// Created by 李洋 on 2023/8/13. +// +#include +using namespace std; +class Q88 { +public: + void merge(vector& nums1, int m, vector& 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]; + } + } +}; \ No newline at end of file diff --git a/23/09/2594.h b/23/09/2594.h new file mode 100644 index 0000000..1817db7 --- /dev/null +++ b/23/09/2594.h @@ -0,0 +1,16 @@ +// +// Created by 李洋 on 2023/9/7. +// + +#ifndef LEECODE_C_2594_H +#define LEECODE_C_2594_H + +#include + +using namespace std; + +long long repairCars(vector &ranks, int cars) { + +} + +#endif //LEECODE_C_2594_H diff --git a/23/09/Q1123.h b/23/09/Q1123.h new file mode 100644 index 0000000..f547a94 --- /dev/null +++ b/23/09/Q1123.h @@ -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 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 diff --git a/23/09/Q2240.h b/23/09/Q2240.h new file mode 100644 index 0000000..30eb6b7 --- /dev/null +++ b/23/09/Q2240.h @@ -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 diff --git a/23/10/2520.h b/23/10/2520.h new file mode 100644 index 0000000..ac46f51 --- /dev/null +++ b/23/10/2520.h @@ -0,0 +1,26 @@ +// +// Created by 李洋 on 2023/10/26. +// + +#ifndef LEECODE_C_2520_H +#define LEECODE_C_2520_H + +#include + +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 diff --git a/23/10/901.h b/23/10/901.h new file mode 100644 index 0000000..a241818 --- /dev/null +++ b/23/10/901.h @@ -0,0 +1,49 @@ +// +// Created by 李洋 on 2023/10/7. +// + +#ifndef LEECODE_C_Q901_H +#define LEECODE_C_Q901_H + +#include + +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 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 \ No newline at end of file diff --git a/23/10/Q1346.h b/23/10/Q1346.h new file mode 100644 index 0000000..e35b94d --- /dev/null +++ b/23/10/Q1346.h @@ -0,0 +1,25 @@ +#ifndef LEECODE_C_Q1346_H +#define LEECODE_C_Q1346_H +#include +using namespace std; + +class Q1346 { +public: + bool checkIfExist(vector& arr) { + unordered_map 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 \ No newline at end of file diff --git a/23/10/Q136.h b/23/10/Q136.h new file mode 100644 index 0000000..6703eea --- /dev/null +++ b/23/10/Q136.h @@ -0,0 +1,23 @@ +// +// Created by 李洋 on 2023/10/14. +// + +#ifndef LEECODE_C_Q136_H +#define LEECODE_C_Q136_H + +#include + +using namespace std; + +class Q136 { +public: + int singleNumber(vector &nums) { + int count = 0; + for (int i: nums) { + count ^= i; + } + return count; + } +}; + +#endif //LEECODE_C_Q136_H diff --git a/23/10/Q137.h b/23/10/Q137.h new file mode 100644 index 0000000..f3e2a7c --- /dev/null +++ b/23/10/Q137.h @@ -0,0 +1,29 @@ +// +// Created by 李洋 on 2023/10/16. +// + +#ifndef LEECODE_C_Q137_H +#define LEECODE_C_Q137_H + +#include +#include + +using namespace std; + +class Q137 { +public: + int singleNumber(vector &nums) { + unordered_map 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 diff --git a/23/10/Q1465.h b/23/10/Q1465.h new file mode 100644 index 0000000..7dc909e --- /dev/null +++ b/23/10/Q1465.h @@ -0,0 +1,41 @@ +// +// Created by 李洋 on 2023/10/27. +// + +#ifndef LEECODE_C_Q1465_H +#define LEECODE_C_Q1465_H + +#include + +using namespace std; + +class Q1465 { +public: + int maxArea(int h, int w, vector &horizontalCuts, vector &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 diff --git a/23/10/Q1488.h b/23/10/Q1488.h new file mode 100644 index 0000000..a68175c --- /dev/null +++ b/23/10/Q1488.h @@ -0,0 +1,44 @@ +// +// Created by 李洋 on 2023/10/13. +// + +#ifndef LEECODE_C_Q1488_H +#define LEECODE_C_Q1488_H + +#include +#include + +using namespace std; + +class Q1488 { +public: + vector avoidFlood(vector &rains) { + map m; + vector 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(); + } + m[rains[i]] = 1; + ret.push_back(-1); + } + return ret; + } +}; + +#endif //LEECODE_C_Q1488_H diff --git a/23/10/Q1726.h b/23/10/Q1726.h new file mode 100644 index 0000000..348e26f --- /dev/null +++ b/23/10/Q1726.h @@ -0,0 +1,77 @@ +// +// Created by 李洋 on 2023/10/19. +// + +#ifndef LEECODE_C_Q1726_H +#define LEECODE_C_Q1726_H + +#include +#include +#include <../../dataStruct/Combination.h> + +using namespace std; + +int tupleSameProduct(vector &nums) { + int count = 0; + unordered_map 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 &nums) { + int count = 0; + vector 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 &nums) { + int count = 0; + vector 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 diff --git a/23/10/Q2525.h b/23/10/Q2525.h new file mode 100644 index 0000000..e477185 --- /dev/null +++ b/23/10/Q2525.h @@ -0,0 +1,29 @@ +// +// Created by 李洋 on 2023/10/20. +// + +#ifndef LEECODE_C_Q2525_H +#define LEECODE_C_Q2525_H + +#include + +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 diff --git a/23/10/Q2526.h b/23/10/Q2526.h new file mode 100644 index 0000000..d0a0d42 --- /dev/null +++ b/23/10/Q2526.h @@ -0,0 +1,33 @@ +// +// Created by 李洋 on 2023/10/12. +// + +#ifndef LEECODE_C_Q2526_H +#define LEECODE_C_Q2526_H + +#include + +using namespace std; + +class Q2526 { +public: + long long findTheArrayConcVal(vector &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 diff --git a/23/10/Q2530.h b/23/10/Q2530.h new file mode 100644 index 0000000..067e2c9 --- /dev/null +++ b/23/10/Q2530.h @@ -0,0 +1,26 @@ +// +// Created by 李洋 on 2023/10/18. +// + +#ifndef LEECODE_C_Q2530_H +#define LEECODE_C_Q2530_H + +#include +#include + +using namespace std; + +long long maxKelements(vector &nums, int k) { + long long count = 0; + priority_queue 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 diff --git a/23/10/Q260.h b/23/10/Q260.h new file mode 100644 index 0000000..986ed52 --- /dev/null +++ b/23/10/Q260.h @@ -0,0 +1,35 @@ +// +// Created by 李洋 on 2023/10/16. +// + +#ifndef LEECODE_C_Q260_H +#define LEECODE_C_Q260_H + +#include +#include + +using namespace std; + +class Q260 { +public: + vector singleNumber(vector &nums) { + unordered_map m; + for (int i: nums) { + m[i] = m[i] + 1; + } + vector 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 diff --git a/23/10/Q2652.h b/23/10/Q2652.h new file mode 100644 index 0000000..895a7f7 --- /dev/null +++ b/23/10/Q2652.h @@ -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 diff --git a/23/10/Q2698.h b/23/10/Q2698.h new file mode 100644 index 0000000..51db38f --- /dev/null +++ b/23/10/Q2698.h @@ -0,0 +1,26 @@ +// +// Created by 李洋 on 2023/10/25. +// + +#ifndef LEECODE_C_Q2698_H +#define LEECODE_C_Q2698_H + +#include + +using namespace std; + +class Q2698 { +public: + int punishmentNumber(int n) { + auto all = vector( + {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 diff --git a/23/10/Q274.h b/23/10/Q274.h new file mode 100644 index 0000000..e221ccb --- /dev/null +++ b/23/10/Q274.h @@ -0,0 +1,30 @@ +// +// Created by 李洋 on 2023/10/29. +// + +#ifndef LEECODE_C_Q274_H +#define LEECODE_C_Q274_H + +#include + +using namespace std; + +class Q274 { +public: + int hIndex(vector &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 diff --git a/23/10/Q275.h b/23/10/Q275.h new file mode 100644 index 0000000..51303ae --- /dev/null +++ b/23/10/Q275.h @@ -0,0 +1,29 @@ +// +// Created by 李洋 on 2023/10/30. +// + +#ifndef LEECODE_C_Q275_H +#define LEECODE_C_Q275_H + +#include + +using namespace std; + +class Q275 { +public: + int hIndex(vector &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 diff --git a/23/10/T368.h b/23/10/T368.h new file mode 100644 index 0000000..0e7d7fa --- /dev/null +++ b/23/10/T368.h @@ -0,0 +1,57 @@ +// +// Created by 李洋 on 2023/10/22. +// + +#ifndef LEECODE_C_T368_H +#define LEECODE_C_T368_H + +#include + +using namespace std; + +int minimumSum(vector &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 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 diff --git a/23/11/Q117.h b/23/11/Q117.h new file mode 100644 index 0000000..a5e811c --- /dev/null +++ b/23/11/Q117.h @@ -0,0 +1,50 @@ +// +// Created by 李洋 on 2023/11/3. +// + +#ifndef LEECODE_C_Q117_H +#define LEECODE_C_Q117_H + +#include +#include + +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> 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 diff --git a/23/11/Q187.h b/23/11/Q187.h new file mode 100644 index 0000000..0c34e8f --- /dev/null +++ b/23/11/Q187.h @@ -0,0 +1,28 @@ +// +// Created by 李洋 on 2023/11/5. +// + +#ifndef LEECODE_C_Q187_H +#define LEECODE_C_Q187_H + +#include +#include +#include + +using namespace std; + +vector findRepeatedDnaSequences(string s) { + vector ret; + unordered_map m; + for(int i = 0;i+9 1){ + ret.push_back(key); + } + } + return ret; +} + +#endif //LEECODE_C_Q187_H diff --git a/23/11/Q2342.h b/23/11/Q2342.h new file mode 100644 index 0000000..6cf5d06 --- /dev/null +++ b/23/11/Q2342.h @@ -0,0 +1,28 @@ +#ifndef LEECODE_C_Q2342_H +#define LEECODE_C_Q2342_H + +#include +#include +using namespace std; + +int maximumSum(vector &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 \ No newline at end of file diff --git a/23/11/Q2586.h b/23/11/Q2586.h new file mode 100644 index 0000000..c6cc840 --- /dev/null +++ b/23/11/Q2586.h @@ -0,0 +1,31 @@ +// +// Created by 李洋 on 2023/11/8. +// + +#ifndef LEECODE_C_Q2586_H +#define LEECODE_C_Q2586_H + +#include +#include +#include + +using namespace std; + +int vowelStrings(vector &words, int left, int right) { + unordered_map 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 diff --git a/23/11/Q2609.h b/23/11/Q2609.h new file mode 100644 index 0000000..6570e0a --- /dev/null +++ b/23/11/Q2609.h @@ -0,0 +1,25 @@ +// +// Created by 李洋 on 2023/11/8. +// + +#ifndef LEECODE_C_Q2609_H +#define LEECODE_C_Q2609_H + +#include +#include +#include + +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 diff --git a/23/11/Q318.h b/23/11/Q318.h new file mode 100644 index 0000000..564062d --- /dev/null +++ b/23/11/Q318.h @@ -0,0 +1,17 @@ +// +// Created by 李洋 on 2023/11/6. +// + +#ifndef LEECODE_C_Q318_H +#define LEECODE_C_Q318_H + +#include +#include + +using namespace std; + +int maxProduct(vector &words) { + +} + +#endif //LEECODE_C_Q318_H diff --git a/23/11/Q876.h b/23/11/Q876.h new file mode 100644 index 0000000..fdbb9b1 --- /dev/null +++ b/23/11/Q876.h @@ -0,0 +1,33 @@ +// +// Created by 李洋 on 2023/11/7. +// + +#ifndef LEECODE_C_Q876_H +#define LEECODE_C_Q876_H + +#include +#include + +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 diff --git a/24/01/2497.h b/24/01/2497.h new file mode 100644 index 0000000..5aa442e --- /dev/null +++ b/24/01/2497.h @@ -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 + +using namespace std; + +//单调栈 +ListNode *removeNodes(ListNode *head) { + stack 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 diff --git a/24/01/670.h b/24/01/670.h new file mode 100644 index 0000000..eac012c --- /dev/null +++ b/24/01/670.h @@ -0,0 +1,33 @@ +// +// Created by 李洋 on 2024/1/22. +// + +#ifndef LEECODE_C_670_H +#define LEECODE_C_670_H + +#include + +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 diff --git a/24/01/82.h b/24/01/82.h new file mode 100644 index 0000000..34c6797 --- /dev/null +++ b/24/01/82.h @@ -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 diff --git a/24/02/235.h b/24/02/235.h new file mode 100644 index 0000000..13e1487 --- /dev/null +++ b/24/02/235.h @@ -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 diff --git a/24/02/429.h b/24/02/429.h new file mode 100644 index 0000000..60e3c9e --- /dev/null +++ b/24/02/429.h @@ -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 diff --git a/24/02/590.h b/24/02/590.h new file mode 100644 index 0000000..96f78e3 --- /dev/null +++ b/24/02/590.h @@ -0,0 +1,41 @@ +// +// Created by 李洋 on 2024/2/19. +// + +#ifndef LEETCODE_C_590_H +#define LEETCODE_C_590_H + +#include + + +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 diff --git a/24/03/322.c b/24/03/322.c new file mode 100644 index 0000000..d36dc40 --- /dev/null +++ b/24/03/322.c @@ -0,0 +1,40 @@ +// +// Created by 李洋 on 2024/3/26. +// +#include +#include +#include + +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); +} diff --git a/24/03/518.c b/24/03/518.c new file mode 100644 index 0000000..773042a --- /dev/null +++ b/24/03/518.c @@ -0,0 +1,27 @@ +// +// Created by 李洋 on 2024/3/26. +// +#include +#include +#include + +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); +} diff --git a/24/03/quicksort.c b/24/03/quicksort.c new file mode 100644 index 0000000..5c902b1 --- /dev/null +++ b/24/03/quicksort.c @@ -0,0 +1,12 @@ +#include + +void swap(int* a, int* b) { + int temp = *a; + *a = *b; + *b = temp; +} + +void QuickSort(int *arr, int low, int high) +{ + +} \ No newline at end of file diff --git a/24/04/1379.cpp b/24/04/1379.cpp new file mode 100644 index 0000000..d53f91c --- /dev/null +++ b/24/04/1379.cpp @@ -0,0 +1,32 @@ +// +// Created by szh2 on 24-4-3. +// +#include + +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); + } +}; \ No newline at end of file diff --git a/24/04/2192.cpp b/24/04/2192.cpp new file mode 100644 index 0000000..4e06e88 --- /dev/null +++ b/24/04/2192.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; +class Solution { +public: + vector> getAncestors(int n, vector> &edges) { + vector> g(n); + for (auto &e : edges) { + g[e[1]].push_back(e[0]); // 反向建图 + } + + vector> ans(n); + vector vis(n); + function 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; + } +}; \ No newline at end of file diff --git a/24/04/2810.c b/24/04/2810.c new file mode 100644 index 0000000..395eb5d --- /dev/null +++ b/24/04/2810.c @@ -0,0 +1,31 @@ +// +// Created by 李洋 on 2024/4/3. +// +#include +#include + +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; +} \ No newline at end of file diff --git a/24/06/520.c b/24/06/520.c new file mode 100644 index 0000000..f569b1b --- /dev/null +++ b/24/06/520.c @@ -0,0 +1,39 @@ +// +// Created by 李洋 on 2024/6/23. +// + +#ifndef LEETCODE_C_520_CPP +#define LEETCODE_C_520_CPP + +#include +#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 diff --git a/24/06/LCP61.h b/24/06/LCP61.h new file mode 100644 index 0000000..0e66852 --- /dev/null +++ b/24/06/LCP61.h @@ -0,0 +1,30 @@ +// +// Created by 李洋 on 2024/6/21. +// + +#ifndef LEETCODE_C_LCP61_H +#define LEETCODE_C_LCP61_H + +# include + +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 diff --git a/24/09/2860.h b/24/09/2860.h new file mode 100644 index 0000000..056e4bb --- /dev/null +++ b/24/09/2860.h @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int countWays(vector &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; +} diff --git a/24/09/70.h b/24/09/70.h new file mode 100644 index 0000000..cda2142 --- /dev/null +++ b/24/09/70.h @@ -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 diff --git a/24/11/3226.cpp b/24/11/3226.cpp new file mode 100644 index 0000000..54d10d8 --- /dev/null +++ b/24/11/3226.cpp @@ -0,0 +1,26 @@ + +int minChanges(int n, int k) +{ + if(n (k & 1)) + { + count++; + } + n >>= 1; + k >>= 1; + } + return count; +} + +int minChanges1(int n, int k) { + return (n & k) == k ? __builtin_popcount(n ^ k) : -1; +} \ No newline at end of file diff --git a/24/11/3243.cpp b/24/11/3243.cpp new file mode 100644 index 0000000..83e538a --- /dev/null +++ b/24/11/3243.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +vector shortestDistanceAfterQueries(int n, vector> &queries) +{ + vector> prev(n); + vector dp(n); + for (int i = 1; i < n; i++) + { + prev[i].push_back(i - 1); + dp[i] = i; + } + vector 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; +} diff --git a/24/11/3258.cpp b/24/11/3258.cpp new file mode 100644 index 0000000..27470c4 --- /dev/null +++ b/24/11/3258.cpp @@ -0,0 +1,36 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/24/11/3259.cpp b/24/11/3259.cpp new file mode 100644 index 0000000..756ce79 --- /dev/null +++ b/24/11/3259.cpp @@ -0,0 +1,51 @@ +#include +#include +#include "math.h" + +using namespace std; + +// 3259. 能量提升 +// 动态规划+贪心 递推式:dp[i] += max(now[i-1], next[i-2]) +long long maxEnergyBoost(vector &energyDrinkA, vector &energyDrinkB) +{ + int n = energyDrinkA.size(); + vector> dp(n + 1, vector(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]); +} \ No newline at end of file diff --git a/24/11/Llist.c b/24/11/Llist.c new file mode 100644 index 0000000..c498c2a --- /dev/null +++ b/24/11/Llist.c @@ -0,0 +1,108 @@ +#include + +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; +} diff --git a/24/11/euclidean.c b/24/11/euclidean.c new file mode 100644 index 0000000..82442c9 --- /dev/null +++ b/24/11/euclidean.c @@ -0,0 +1,66 @@ +#include +#include + +#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; +} \ No newline at end of file diff --git a/24/11/length.cpp b/24/11/length.cpp new file mode 100644 index 0000000..0e27e72 --- /dev/null +++ b/24/11/length.cpp @@ -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; +} \ No newline at end of file diff --git a/24/11/tree.c b/24/11/tree.c new file mode 100644 index 0000000..3e54963 --- /dev/null +++ b/24/11/tree.c @@ -0,0 +1,219 @@ +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/24/12/3001.cpp b/24/12/3001.cpp new file mode 100644 index 0000000..1c60d52 --- /dev/null +++ b/24/12/3001.cpp @@ -0,0 +1,18 @@ +#include +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; +} \ No newline at end of file diff --git a/25/05/2131.go b/25/05/2131.go new file mode 100644 index 0000000..8a73c5d --- /dev/null +++ b/25/05/2131.go @@ -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 +} diff --git a/25/05/2894.go b/25/05/2894.go new file mode 100644 index 0000000..a2467b5 --- /dev/null +++ b/25/05/2894.go @@ -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 +} diff --git a/25/05/2942.go b/25/05/2942.go new file mode 100644 index 0000000..f992e61 --- /dev/null +++ b/25/05/2942.go @@ -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 +} diff --git a/25/06/1061.go b/25/06/1061.go new file mode 100644 index 0000000..3924e27 --- /dev/null +++ b/25/06/1061.go @@ -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) +} diff --git a/25/06/1432.go b/25/06/1432.go new file mode 100644 index 0000000..789a4c8 --- /dev/null +++ b/25/06/1432.go @@ -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 +} diff --git a/25/06/2929.go b/25/06/2929.go new file mode 100644 index 0000000..8532b92 --- /dev/null +++ b/25/06/2929.go @@ -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 +} diff --git a/25/06/3423.go b/25/06/3423.go new file mode 100644 index 0000000..773a98a --- /dev/null +++ b/25/06/3423.go @@ -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 +} diff --git a/25/06/3442.go b/25/06/3442.go new file mode 100644 index 0000000..0928aba --- /dev/null +++ b/25/06/3442.go @@ -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 +} diff --git a/25/06/386.go b/25/06/386.go new file mode 100644 index 0000000..4cedbdb --- /dev/null +++ b/25/06/386.go @@ -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 +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..120b6a4 --- /dev/null +++ b/README.md @@ -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。 \ No newline at end of file diff --git a/dataStruct/Combination.h b/dataStruct/Combination.h new file mode 100644 index 0000000..cd80942 --- /dev/null +++ b/dataStruct/Combination.h @@ -0,0 +1,35 @@ +// +// Created by 李洋 on 2023/10/19. +// + +#ifndef LEECODE_C_COMBINATION_H +#define LEECODE_C_COMBINATION_H + +#include +#include + +// 计算组合数 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> dp(n + 1, std::vector(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 diff --git a/dataStruct/Heap/Heap.h b/dataStruct/Heap/Heap.h new file mode 100644 index 0000000..a47f239 --- /dev/null +++ b/dataStruct/Heap/Heap.h @@ -0,0 +1,23 @@ +// +// Created by 李洋 on 2023/8/12. +// + +#ifndef LEECODE_C_HEAP_H +#define LEECODE_C_HEAP_H + +#include "vector" +template +class Heap { +private: + std::vector array; +public: + int length; + + Heap(std::vector &array) : array(array){ + length = array.size(); + } + + typedef void (Heap::*compare(T a,T b))(bool); +}; + +#endif //LEECODE_C_HEAP_H diff --git a/dataStruct/LinkedList/lists.h b/dataStruct/LinkedList/lists.h new file mode 100644 index 0000000..9497af0 --- /dev/null +++ b/dataStruct/LinkedList/lists.h @@ -0,0 +1,66 @@ +// +// Created by 李洋 on 2023/8/6. +// + +#ifndef LEECODE_C_LISTS_H +#define LEECODE_C_LISTS_H +#pragma once + +#include +#include + +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 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 diff --git a/dataStruct/Queue/PriorityQueue.c b/dataStruct/Queue/PriorityQueue.c new file mode 100644 index 0000000..f1604e9 --- /dev/null +++ b/dataStruct/Queue/PriorityQueue.c @@ -0,0 +1,128 @@ +#include +#include + +#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; +} diff --git a/dataStruct/Queue/README.md b/dataStruct/Queue/README.md new file mode 100644 index 0000000..03d0549 --- /dev/null +++ b/dataStruct/Queue/README.md @@ -0,0 +1,15 @@ +# QUEUE + +## PriorityQueue - 优先队列 + +- 优先队列也是一种队列,只不过不同的是,优先队列的出队顺序是按照优先级来的,可能需要找到元素集合中的最小或者最大元素,可以利用优先队列ADT来完成操作,优先队列ADT是一种数据结构,它支持插入和删除最小值操作(返回并删除最小元素)或删除最大值操作(返回并删除最大元素); + +- 这些操作等价于队列的`enQueue`和`deQueue`操作,区别在于,对于优先队列,元素进入队列的顺序可能与其被操作的顺序不同,作业调度是优先队列的一个应用实例,它根据优先级的高低而不是先到先服务的方式来进行调度; +- 如果最小键值元素拥有最高的优先级,那么这种优先队列叫作**升序优先队列**(即总是先删除最小的元素),类似的,如果最大键值元素拥有最高的优先级,那么这种优先队列叫作**降序优先队列**(即总是先删除最大的元素);由于这两种类型时对称的,所以只需要关注其中一种,如升序优先队列; +- 优先队列的应用 + - 数据压缩:赫夫曼编码算法; + - 最短路径算法:Dijkstra算法; + - 最小生成树算法:Prim算法; + - 事件驱动仿真:顾客排队算法; + - 选择问题:查找第k个最小元素; + - 等等等等.... diff --git a/dataStruct/Tree/Tree.h b/dataStruct/Tree/Tree.h new file mode 100644 index 0000000..edeaf93 --- /dev/null +++ b/dataStruct/Tree/Tree.h @@ -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 +#include +#include + +TreeNode *creatRandomTree(int size) { + if (size == 0) { + return nullptr; + } + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dis(1, 100); + + std::queue Q; + std::queue 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 diff --git a/dataStruct/Tree/TreeStack.h b/dataStruct/Tree/TreeStack.h new file mode 100644 index 0000000..70472c4 --- /dev/null +++ b/dataStruct/Tree/TreeStack.h @@ -0,0 +1,56 @@ +// +// Created by 李洋 on 2023/9/6. +// + +#ifndef LEECODE_C_TREESTACK_H +#define LEECODE_C_TREESTACK_H +#pragma once + +#include +#include +#include "./Tree.h" + +class TreeStack { +private: + std::vector 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 diff --git a/dynamic planning/*22.cpp b/dynamic planning/*22.cpp new file mode 100644 index 0000000..a5bffa2 --- /dev/null +++ b/dynamic planning/*22.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +class Solution { + public: + vector generateParenthesis(int n) { + vector v; + dfs(v, "", n, n); + return v; + } + void dfs(vector &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); + } + } + }; \ No newline at end of file diff --git a/else/DivConvert.h b/else/DivConvert.h new file mode 100644 index 0000000..16ef78a --- /dev/null +++ b/else/DivConvert.h @@ -0,0 +1,29 @@ +// +// Created by 李洋 on 2023/10/2. +// + +#ifndef LEECODE_C_DIVCONVERT_H +#define LEECODE_C_DIVCONVERT_H + +#include +#include + +using namespace std; + +string decimalToBinaryUsingEuclidean(int target) { + stack 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 diff --git a/else/Heap/README.md b/else/Heap/README.md new file mode 100644 index 0000000..e69de29 diff --git a/else/Heap/test.h b/else/Heap/test.h new file mode 100644 index 0000000..7945605 --- /dev/null +++ b/else/Heap/test.h @@ -0,0 +1,34 @@ +// +// Created by 李洋 on 2023/10/18. +// + +#ifndef LEECODE_C_TEST_H_P +#define LEECODE_C_TEST_H_P + +#include +#include + +using namespace std; + +void testPQ() { + priority_queue, 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, greater<>> minHeap; + minHeap.push(20); + minHeap.push(10); + minHeap.push(30); + cout << minHeap.top() << endl; +} + +#endif //LEECODE_C_TEST_H_P diff --git a/else/README.md b/else/README.md new file mode 100644 index 0000000..28d4ac4 --- /dev/null +++ b/else/README.md @@ -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) + + + diff --git a/else/demo/1.h b/else/demo/1.h new file mode 100644 index 0000000..27aebd1 --- /dev/null +++ b/else/demo/1.h @@ -0,0 +1,31 @@ +// +// Created by 李洋 on 2024/1/8. +// + +#ifndef LEECODE_C_1_H +#define LEECODE_C_1_H + +#include +#include +#include + +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 b; //c++ 向量,可以看作是动态数组,自动扩充容量 + vector> c; //二维动态数组 + b.push_back(1); + b.push_back(2); + b.size(); //获取当前长度 +} + +#endif //LEECODE_C_1_H diff --git a/else/demo/2.h b/else/demo/2.h new file mode 100644 index 0000000..43dde73 --- /dev/null +++ b/else/demo/2.h @@ -0,0 +1,24 @@ +// +// Created by 李洋 on 2024/1/11. +// + +#ifndef LEECODE_C_2_H +#define LEECODE_C_2_H + +#include + +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 diff --git a/else/kuohao.h b/else/kuohao.h new file mode 100644 index 0000000..cbbb653 --- /dev/null +++ b/else/kuohao.h @@ -0,0 +1,52 @@ +// +// Created by 李洋 on 2023/10/4. +// + +#ifndef LEECODE_C_KUOHAO_H +#define LEECODE_C_KUOHAO_H + +#include +#include + +bool isKuoHao(std::string target) { + std::stack 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 diff --git a/else/main.h b/else/main.h new file mode 100644 index 0000000..6faf145 --- /dev/null +++ b/else/main.h @@ -0,0 +1,31 @@ +// +// Created by 李洋 on 2023/10/12. +// + +#ifndef LEECODE_C_MAIN_H +#define LEECODE_C_MAIN_H + +#include +#include +#include "./structs/Tree.h" +#include "./traverse.h" +#include "./Heap/test.h" +#include "./sorts.h" +#include "./structs/Graph.h" +#include "./topic/2019.h" +#include + +using namespace std; + +void run() +{ + vector arr({3, 2, 2, 4, 6, 2, 3}); + odd_even(arr); + for (int i : arr) + { + cout << i << " "; + } + cout << endl; +} + +#endif // LEECODE_C_MAIN_H diff --git a/else/sorts.h b/else/sorts.h new file mode 100644 index 0000000..ac2b631 --- /dev/null +++ b/else/sorts.h @@ -0,0 +1,171 @@ +// +// Created by 李洋 on 2023/10/19. +// + +#ifndef LEECODE_C_SHELLSORT_H +#define LEECODE_C_SHELLSORT_H + +#include +#include +#include + +using namespace std; + +void shellSort(vector &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 &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 &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 &arr) { + splits(arr, 0, arr.size() - 1); +} +// 快速排序 - end + +// 堆排序 - start small -> large +void maxHeapify(vector &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 &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& arr, int left, int middle, int right) { + int n1 = middle - left + 1; + int n2 = right - middle; + + std::vector leftArr(n1); + std::vector 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& 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 dis(1, 100); + vector 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 diff --git a/else/structs/Graph.h b/else/structs/Graph.h new file mode 100644 index 0000000..37b313e --- /dev/null +++ b/else/structs/Graph.h @@ -0,0 +1,181 @@ +// +// Created by 李洋 on 2023/10/21. +// 以邻接矩阵实现的INT图及其具体操作 +// + +#ifndef LEECODE_C_GRAPH_H +#define LEECODE_C_GRAPH_H + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Graph { +public: + int size; + + Graph(int n, bool direction) { + this->size = n; + G = vector>(size, vector(size, 0)); + visited = vector(size, 0); + + random_device rd; + mt19937 gen(rd()); + uniform_int_distribution 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>(size, vector(size, 0)); + visited = vector(size, 0); + vector> 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> &Edges) { + for (auto Edge: Edges) { + vertex.insert(Edge[1]); + vertex.insert(Edge[2]); + } + + size = vertex.size(); + G = vector>(size, vector(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 DFS(int start) { + queue Q; + stack> 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 BFS(int start) { + queue Q; + queue> 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> G; // adjacent matrix + vector visited; + + set vertex; // The collection of vertex + set> ccp; // The graph has multiple connected components. + + +}; + +#endif //LEECODE_C_GRAPH_H diff --git a/else/structs/List.h b/else/structs/List.h new file mode 100644 index 0000000..2064848 --- /dev/null +++ b/else/structs/List.h @@ -0,0 +1,8 @@ +#ifndef List_h +#define List_h +struct ListNode{ + int value; + struct ListNode * next; +} + +#endif //List_h \ No newline at end of file diff --git a/else/structs/Tree.h b/else/structs/Tree.h new file mode 100644 index 0000000..e8677e4 --- /dev/null +++ b/else/structs/Tree.h @@ -0,0 +1,94 @@ +// +// 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 +#include +#include + +TreeNode *createRandomTree(int size) +{ + if (size == 0) + { + return nullptr; + } + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dis(1, 100); + + std::queue Q; + std::queue 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; +} + +void lookTree(TreeNode *root) +{ + std::queue q; + q.push(root); + while (!q.empty()) + { + TreeNode *head = q.front(); + q.pop(); + std::cout << head->val << " "; + if (head->left) + { + q.push(head->left); + } + if (head->right) + { + q.push(head->right); + } + } +} + +#endif // LEECODE_C_TREE_H diff --git a/else/symmetry.h b/else/symmetry.h new file mode 100644 index 0000000..93b29d5 --- /dev/null +++ b/else/symmetry.h @@ -0,0 +1,30 @@ +// +// Created by 李洋 on 2023/10/4. +// + +#ifndef LEECODE_C_SYMMETRY_H +#define LEECODE_C_SYMMETRY_H + +#include +#include + +bool isSymmetry(std::string target) { + std::stack tk; + for (int i = 0; i < target.length(); ++i) { + if (i < target.length() / 2) { + tk.push(target[i]); + } + if (i == target.length() / 2 && target.length() % 2 == 1) { + i++; + } + if (i >= target.length() / 2) { + if (target[i] != tk.top()) { + return false; + } + tk.pop(); + } + } + return true; +} + +#endif //LEECODE_C_SYMMETRY_H diff --git a/else/test1.h b/else/test1.h new file mode 100644 index 0000000..dc4a29e --- /dev/null +++ b/else/test1.h @@ -0,0 +1,66 @@ +// +// Created by 李洋 on 2023/12/17. +// + +#ifndef LEECODE_C_TEST1_H +#define LEECODE_C_TEST1_H + +#include "stdlib.h" + +typedef struct LinkNode { + char value; + struct LinkNode *next; +} *Node; + +struct p { + int length; + Node head; +}; + +p create(Node l1) { + if (!l1) { + return p{0, nullptr}; //NULL + } + + Node temp = (Node) malloc(sizeof(LinkNode)); + temp->value = l1->value; + l1 = l1->next; + + Node head = temp; + + int length = 1; + while (l1) { + temp = (Node) malloc(sizeof(LinkNode)); + temp->value = l1->value; + temp->next = head; + l1 = l1->next; + head = temp; + length++; + } + return p{length, head}; +} + +Node test(Node l1, Node l2) { + p p1 = create(l1); + p p2 = create(l2); + + Node n1 = p1.head; + Node n2 = p2.head; + + int index = 0; + while (n1->value == n2->value) { + n1 = n1->next; + n2 = n2->next; + index++; + } + + Node ps = l1; + int length = p1.length - index; + while (length--) { + ps = ps->next; + } + + return ps; +} + +#endif //LEECODE_C_TEST1_H diff --git a/else/topic/2017.h b/else/topic/2017.h new file mode 100644 index 0000000..54a9d74 --- /dev/null +++ b/else/topic/2017.h @@ -0,0 +1,76 @@ +#include "../structs/Tree.h" +#include + +using namespace std; + +void reversechild(TreeNode *root) +{ + TreeNode *head = root; + queue q; + q.push(head); + while (!q.empty()) + { + head = q.front(); + q.pop(); + if (head->left) + { + q.push(head->left); + } + if (head->right) + { + q.push(head->right); + } + swap(head->left, head->right); + } +} + +#include +#include + +using namespace std; + +unordered_map statistic(vector L1) +{ + unordered_map m; + for (auto i : L1) + { + m[i] = m[i] + 1; + } + return m; +} + +#include "../structs/List.h" + +ListNode *integrateList(LitNode *L1, ListNode *l2) +{ + ListNode *head = L1->value < L2->value ? L1 : L2; + if (L1->value < L2->value) + { + L1 = L1->next; + } + else + { + L2 = L2->next; + } + ListNode *temp = head->next; + while (L1 && L2) + { + temp->next = L1->value < L2->value ? L1 : L2; + if (L1->value < L2->value) + { + L1 = L1->next; + } + else + { + L2 = L2->next; + } + temp = temp->next; + } + if(L1){ + temp->next = L1; + } + if(L2){ + temp->next = L2; + } + return head; +} diff --git a/else/topic/2019.h b/else/topic/2019.h new file mode 100644 index 0000000..19bc12c --- /dev/null +++ b/else/topic/2019.h @@ -0,0 +1,60 @@ +#include +using namespace std; + +void odd_even(vector &array) +{ + int left = 0; + int right = array.size() - 1; + while (left < right) + { + while (left < right && array[right] % 2 == 0) + { + right--; + } + while (left < right && array[left] % 2 == 1) + { + left++; + } + swap(array[left], array[right]); + } +} + +#include "../structs/Tree.h" + +void insertSearchTree(TreeNode *root, int value) +{ + TreeNode *dist = new TreeNode(); + dist->val = value; + if (!root) + { + root = dist; + } + TreeNode *temp = root; + while (temp) + { + if (temp->val > value) + { + if (temp->left) + { + temp = temp->left; + continue; + } + temp->left = dist; + break; + } + if (temp->val < value) + { + if (temp->right) + { + temp = temp->right; + continue; + } + temp->left = dist; + break; + } + if (temp->val == value) + { + break; + } + } +} \ No newline at end of file diff --git a/else/topic/2020.h b/else/topic/2020.h new file mode 100644 index 0000000..7896edb --- /dev/null +++ b/else/topic/2020.h @@ -0,0 +1,28 @@ +#include "../structs/Tree.h" + +using namespace std; + +TreeNode *getX(TreeNode *root, int x) +{ + TreeNode *temp = root; + while (temp) + { + if (temp->val > x) + { + if (temp->left) + { + temp = temp->left; + continue; + } + } + else + { + if (temp->right) + { + temp = temp->right; + continue; + } + } + return temp; + } +} \ No newline at end of file diff --git a/else/topic/2021.h b/else/topic/2021.h new file mode 100644 index 0000000..705bed1 --- /dev/null +++ b/else/topic/2021.h @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +int test1(vector array) +{ + if (array.empty()) + { + return INT_MIN; + } + stack S; + int norm = array[0]; + int m = INT_MIN; + S.push(array[0]); + for (int i = 1; i < array.size(); i++) + { + m = max(m, norm - array[i]); + while (!S.empty() && array[i] > S.top()) + { + S.pop(); + } + if (S.empty()) + { + norm = array[i]; + } + S.push(array[i]); + } + return m; +} + +#include "../structs/Tree.h" +int test2(TreeNode *root){ + //先镜像,再按需遍历 +} \ No newline at end of file diff --git a/else/topic/2022.h b/else/topic/2022.h new file mode 100644 index 0000000..72710af --- /dev/null +++ b/else/topic/2022.h @@ -0,0 +1,66 @@ +#include "../structs/List.h" +#include + +using namespace std; + +vector> test1(ListNode *head) +{ + vector> ret; + + int prev = head->value; + int index = 0; + ret.push_back(vector(1, prev)); + + ListNode *temp = head->next; + + while (!temp) + { + if (prev == temp->value) + { + index++; + } + else + { + index = 0; + } + if (index >= ret.size()) + { + ret.push_back(vector(1, temp->value)); + } + if (index < ret.size()) + { + ret[index].push_back(temp->value); + } + + temp = temp->next; + } + + return ret; +} + +#include "../structs/Tree.h" + +pair help(TreeNode *root) +{ + if (!root) + { + return {true, 0}; + } + auto p1 = help(root->left); + auto p2 = help(root->right); + if (!p1.first || !p2.first) + { + return {false, 0}; + } + if (abs(p1.second - p2.second) > 1) + { + return {false, 0}; + } + + return {true, max(p1.second, p2.second) + 1}; +} + +bool test2(TreeNode *root) +{ + return help(root).first; +} \ No newline at end of file diff --git a/else/topic/2023.h b/else/topic/2023.h new file mode 100644 index 0000000..f04ce79 --- /dev/null +++ b/else/topic/2023.h @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +bool test1(vector array){ + stack S; + for (int i = 0; i < array.size()/2; i++) + { + S.push(array[i]); + } + int tag = array.size()%2; + for(int i = array.size()/2 + tag;i S) { + stack temp; + int num = 0; + int i = 0; + while (!S.empty()) { + num += S.top()->value * pow(10, i++); + S.pop(); + } + return num; +} + +int get(Node *root) { + if (!root) { + return 0; + } + int count = 0; + stack S; + S.push(root); + while (!S.empty()) { + if (!S.top()->left && !S.top()->right) { + count += calculate(S); + } + if (S.top()->tag1 == 0 && S.top()->left) { + S.top()->tag1 = 1; + S.push(S.top()->left); + continue; + } + if (S.top()->tag2 == 0 && S.top()->left) { + S.top()->tag2 = 1; + S.push(S.top()->right); + continue; + } + S.pop(); + } + return count; +} + + +#endif //LEECODE_C_2024_H diff --git a/else/traverse.h b/else/traverse.h new file mode 100644 index 0000000..06c8be7 --- /dev/null +++ b/else/traverse.h @@ -0,0 +1,138 @@ +// +// Created by 李洋 on 2023/10/12. +// + +#ifndef LEECODE_C_TRAVERSE_H +#define LEECODE_C_TRAVERSE_H +using namespace std; + +#include "structs/Tree.h" +#include +#include + +queue Q; + +// 递归写法 - int型 +// 先序遍历 preorder traversal +void preorder_recursion_help(TreeNode *head) { + if (head != nullptr) { + Q.push(head); + preorder_recursion_help(head->left); + preorder_recursion_help(head->right); + } +} + +queue preorder_recursion(TreeNode *head) { + preorder_recursion_help(head); + return Q; +}; + +// 中序遍历 inorder traversal +void inorder_recursion_help(TreeNode *head) { + if (head != nullptr) { + inorder_recursion_help(head->left); + Q.push(head); + inorder_recursion_help(head->right); + } +} + +queue inorder_recursion(TreeNode *head) { + inorder_recursion_help(head); + return Q; +}; + +// 后序遍历 postorder traversal +void postorder_recursion_help(TreeNode *head) { + if (head != nullptr) { + postorder_recursion_help(head->left); + postorder_recursion_help(head->right); + Q.push(head); + } +} + +queue postorder_recursion(TreeNode *head) { + postorder_recursion_help(head); + return Q; +}; + +// 非递归写法 +// 先序遍历 preorder traversal +queue preorder(TreeNode *head) { + if (!head) { + return Q; + } + + auto temp = head; + + stack < TreeNode * > S; + + S.push(temp); + + TreeNode *t; + + while (!S.empty()) { + t = S.top(); + S.pop(); + Q.push(t); + if (t->right) + S.push(t->right); + if (t->left) + S.push(t->left); + } + return Q; +} + +// 中序遍历 inorder traversal +queue inorder(TreeNode *head) { + auto temp = head; + stack < TreeNode * > S; + + while (temp != nullptr || !S.empty()) { + if (temp) { + S.push(temp); + temp = temp->left; + continue; + } + temp = S.top(); + Q.push(temp); + S.pop(); + temp = temp->right; + } + return Q; +} + +// 后序遍历 postorder traversal +queue postorder(TreeNode *head) { + auto temp = head; + stack > S; + + while (temp != nullptr || !S.empty()) { + if (temp) { + S.emplace(temp, false); + temp = temp->left; + continue; + } + temp = S.top().first; + + if (temp->right == nullptr || S.top().second) { + Q.push(temp); + S.pop(); + temp = nullptr; + continue; + } + S.top().second = true; + temp = temp->right; + } + return Q; +} + +void test1() { + auto TreeNode = createRandomTree(10); + auto Q = postorder(TreeNode); + while (!Q.empty()) { + cout << Q.front()->val << " "; + Q.pop(); + } +} + +#endif //LEECODE_C_TRAVERSE_H diff --git a/greed/11.h b/greed/11.h new file mode 100644 index 0000000..7a5f17c --- /dev/null +++ b/greed/11.h @@ -0,0 +1,18 @@ +#include + +// 核心优化,每次移动较小的那个,因为移动较大的那个,面积只会越来越小 +int maxArea(int* height, int heightSize) { + int left = 0; + int right = heightSize - 1; + int max = 0; + while(left < right){ + max = fmax(max,(right-left)*fmin(height[left],height[right])); + if (height[left] < height[right]) + { + left++; + }else{ + right--; + } + } + return max; +} \ No newline at end of file diff --git a/greed/11.py b/greed/11.py new file mode 100644 index 0000000..3f42314 --- /dev/null +++ b/greed/11.py @@ -0,0 +1,13 @@ +from typing import List + +class Solution: + def maxArea(self, height: List[int]) -> int: + left, right = 0,len(height)-1 + res = 0 + while left < right: + res = max(res, (right-left) * min(height[left], height[right])) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return res \ No newline at end of file diff --git a/greed/134.h b/greed/134.h new file mode 100644 index 0000000..ddaeb63 --- /dev/null +++ b/greed/134.h @@ -0,0 +1,22 @@ +int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) { + int i = 0; + while (i < gasSize) { + int sumOfGas = 0, sumOfCost = 0; + int cnt = 0; + while (cnt < gasSize) { + int j = (i + cnt) % gasSize; + sumOfGas += gas[j]; + sumOfCost += cost[j]; + if (sumOfCost > sumOfGas) { + break; + } + cnt++; + } + if (cnt == gasSize) { + return i; + } else { + i = i + cnt + 1; + } + } + return -1; +} \ No newline at end of file diff --git a/greed/179.h b/greed/179.h new file mode 100644 index 0000000..2667d43 --- /dev/null +++ b/greed/179.h @@ -0,0 +1,28 @@ +#include + +long cmp(int *x, int *y) { + unsigned long sx = 10, sy = 10; + while (sx <= *x) { + sx *= 10; + } + while (sy <= *y) { + sy *= 10; + } + return sx * (*y) + (*x) - sy * (*x) - (*y); +} + +char *largestNumber(int *nums, int numsSize) { + qsort(nums, numsSize, sizeof(int), cmp); + if (nums[0] == 0) { + char *ret = malloc(sizeof(char) * 2); + ret[0] = '0', ret[1] = '\0'; + return "0"; + } + char *ret = malloc(sizeof(char) * 1000); + char *p = ret; + for (int i = 0; i < numsSize; i++) { + sprintf(p, "%d", nums[i]); + p += strlen(p); + } + return ret; +} \ No newline at end of file diff --git a/greed/316.h b/greed/316.h new file mode 100644 index 0000000..064aa56 --- /dev/null +++ b/greed/316.h @@ -0,0 +1,31 @@ +#include +#include +char* removeDuplicateLetters(char* s) { + int vis[26], num[26]; + memset(vis, 0, sizeof(vis)); + memset(num, 0, sizeof(num)); + + int n = strlen(s); + for (int i = 0; i < n; i++) { + num[s[i] - 'a']++; + } + + char* stk = (char*)malloc(sizeof(char) * 27); + int stkTop = 0; + for (int i = 0; i < n; i++) { + if (!vis[s[i] - 'a']) { + while (stkTop > 0 && stk[stkTop - 1] > s[i]) { + if (num[stk[stkTop - 1] - 'a'] > 0) { + vis[stk[--stkTop] - 'a'] = 0; + } else { + break; + } + } + vis[s[i] - 'a'] = 1; + stk[stkTop++] = s[i]; + } + num[s[i] - 'a'] -= 1; + } + stk[stkTop] = '\0'; + return stk; +} diff --git a/greed/324.h b/greed/324.h new file mode 100644 index 0000000..e04c446 --- /dev/null +++ b/greed/324.h @@ -0,0 +1,17 @@ +static int cmp(const void *pa, const void *pb) { + return *(int *)pa - *(int *)pb; +} + +void wiggleSort(int* nums, int numsSize) { + int * arr = (int *)malloc(sizeof(int) * numsSize); + memcpy(arr, nums, sizeof(int) * numsSize); + qsort(arr, numsSize, sizeof(int), cmp); + int x = (numsSize + 1) / 2; + for (int i = 0, j = x - 1, k = numsSize - 1; i < numsSize; i += 2, j--, k--) { + nums[i] = arr[j]; + if (i + 1 < numsSize) { + nums[i + 1] = arr[k]; + } + } + free(arr); +} \ No newline at end of file diff --git a/greed/409.h b/greed/409.h new file mode 100644 index 0000000..5968832 --- /dev/null +++ b/greed/409.h @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +class Solution { + public: + int longestPalindrome(string s) { + unordered_map count; + int ans = 0; + for (char c : s) + ++count[c]; + for (auto p : count) { + int v = p.second; + ans += v / 2 * 2; + if (v % 2 == 1 and ans % 2 == 0) + ++ans; + } + return ans; + } + }; \ No newline at end of file diff --git a/greed/581.h b/greed/581.h new file mode 100644 index 0000000..82d58e8 --- /dev/null +++ b/greed/581.h @@ -0,0 +1,20 @@ +#include + +int findUnsortedSubarray(int* nums, int numsSize) { + int n = numsSize; + int maxn = INT_MIN, right = -1; + int minn = INT_MAX, left = -1; + for (int i = 0; i < n; i++) { + if (maxn > nums[i]) { + right = i; + } else { + maxn = nums[i]; + } + if (minn < nums[n - i - 1]) { + left = n - i - 1; + } else { + minn = nums[n - i - 1]; + } + } + return right == -1 ? 0 : right - left + 1; +} \ No newline at end of file diff --git a/greed/lcs01.h b/greed/lcs01.h new file mode 100644 index 0000000..5b8f503 --- /dev/null +++ b/greed/lcs01.h @@ -0,0 +1,16 @@ +int leastMinutes(int n){ + int i = 1; + int j = 1; + while(j < n){ + j <<= 1; + i++; + } + return i; +} + +#include + +int leastMinutes(int n) { + if (n <= 0) return 0; + return static_cast(std::ceil(std::log2(n))) + 1; +} diff --git a/greed/lcs01.py b/greed/lcs01.py new file mode 100644 index 0000000..5fb4a86 --- /dev/null +++ b/greed/lcs01.py @@ -0,0 +1,6 @@ +import math + +def least_minutes(n: int) -> int: + if n == 0: + return 0 + return math.ceil(math.log2(n)) + 1 diff --git a/key/2235.md b/key/2235.md new file mode 100644 index 0000000..ab8f283 --- /dev/null +++ b/key/2235.md @@ -0,0 +1,33 @@ +# 两整数相加 +[具体算法](../23/08/Q2235.cpp) +## 位运算 +- 不使用加法运算符 + +假设 num1i 和 num2i分别表示`num1` 和 `num2` 的第 iii 个二进制位。一共有 4 种情况: +​ + +| num1i | num2i | 不进位的和⊕ | 进位(&) | +| :--------------: | :--------------: | :---------: | ------- | +| 0 | 0 | 0 | 0 | +| 0 | 1 | 1 | 0 | +| 1 | 0 | 1 | 0 | +| 1 | 1 | 0 | 1 | + +观察可以发现,“不进位的和”与“异或运算”有相同规律,而进位则与“与”运算规律相同,并且需要左移一位。 + +因此: + +- 对两数进行按位与运算 &&,然后左移一位,得到进位,记为 carry; +- 对两数进行按位异或运算 ⊕,得到不进位的和; +- 问题转换为求:“不进位的数 + 进位” 之和; +- 循环,直至第二个数为 0,返回第一个数即可(也可以用递归实现)。 + + + +## 对于 C++ + +- 将先算出两数中二进制需要进位的点,然后二进制左移一位,相当于算出了所有需要进位的位,并且把这个位转换为了其下一位的值,后面只需要把结果和这个值相加即可。 +- 然后将两树中二进制不需要进位的点相加,也就是 0,1 和 1,0 两种,使用异或操作就可加为1,并保存到num1中方便下一次运算。 +- 此时num2就没用了,他的所有位都被计算过了,此时就可以把num2换成计算进位的时候所保存的值 +- 再进入下一轮运算时,现在的num1相当于是上一轮运算时num1加上了num2的所有无需进位的值,num2相当于是两数相加时所有需要进位的值变成了进位后的值,然后这一轮在相加重复步骤即可。 +- 由于num2在每一轮最后的值都是与操作再进一,相当于进位值,最后总有无须进位的时候,此时num2会变为0,因此循环出口就是num2变为0的时候 \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a3dd973 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, World!") +} diff --git a/test/a_i.c b/test/a_i.c new file mode 100644 index 0000000..5ca2a6b --- /dev/null +++ b/test/a_i.c @@ -0,0 +1,10 @@ +#include +int main(void){ + int a[10],i; + for (i = 1; i <= 10; i++) + { + a[i]=0; + printf("%d\n",i); + } + return 0; +} \ No newline at end of file diff --git a/test/example.txt b/test/example.txt new file mode 100644 index 0000000..70fe388 --- /dev/null +++ b/test/example.txt @@ -0,0 +1 @@ +hello, World!hello, World1!hello, World1! \ No newline at end of file diff --git a/test/file.c b/test/file.c new file mode 100644 index 0000000..1007b7c --- /dev/null +++ b/test/file.c @@ -0,0 +1,17 @@ +#include +#include + +int main(){ + FILE *fp = fopen("example.txt","r+"); + if(fp == NULL){ + printf("error,cannot open file"); + return 1; + } + char buffer[100]; + fread(buffer,sizeof(char),100,fp); + printf("%s\n",buffer); + char *wt = "hello, World1!"; + fwrite(wt,sizeof(char),strlen(wt),fp); + + fclose(fp); +} \ No newline at end of file diff --git a/test/test1.c b/test/test1.c new file mode 100644 index 0000000..88dfd37 --- /dev/null +++ b/test/test1.c @@ -0,0 +1,7 @@ +#include + +int main(){ + char* a= "I love China!"; + a = a+7; + printf("%s\n",a); +} \ No newline at end of file diff --git a/test/test2.c b/test/test2.c new file mode 100644 index 0000000..155d42b --- /dev/null +++ b/test/test2.c @@ -0,0 +1,10 @@ +#include + +int main(){ + int i,j; + i = 16; + j = (i++)+i; + printf("%d\n",j); + i = 15; + printf("%d %d\n",++i,i); +} \ No newline at end of file diff --git a/test/test3.c b/test/test3.c new file mode 100644 index 0000000..8f78182 --- /dev/null +++ b/test/test3.c @@ -0,0 +1,10 @@ +#include + +int main(){ + int i; + char *s = "a\045+45\'b"; + for(i=0;s++;i++); + printf("%d\n",i); +} + +// 无限循环 diff --git a/test/test3305.c b/test/test3305.c new file mode 100644 index 0000000..9f2c7c1 --- /dev/null +++ b/test/test3305.c @@ -0,0 +1,6 @@ +#include "../2025/03/3305.h" +#include + +int main(){ + printf("%d\n",countOfSubstrings("ieaouqqieaouqq",1)); +} \ No newline at end of file diff --git a/tools.h b/tools.h new file mode 100644 index 0000000..a398d08 --- /dev/null +++ b/tools.h @@ -0,0 +1,14 @@ +// +// Created by 李洋 on 2023/10/26. +// + +#ifndef LEECODE_C_TOOLS_H +#define LEECODE_C_TOOLS_H + +#include +#include + +using namespace std; + + +#endif //LEECODE_C_TOOLS_H diff --git a/tools/map.h b/tools/map.h new file mode 100644 index 0000000..c483237 --- /dev/null +++ b/tools/map.h @@ -0,0 +1,109 @@ +// 使用冲突链表实现的哈希表 +#ifndef LISTMAP +#define LISTMAP + +#include +#include + +int getPri(int n) { + int primes[] = { + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, + 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, + 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, + 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, + 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, + 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, + 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, + 353, 359, 367, 373, 379, 383, 389, 397 + }; + int len = sizeof(primes) / sizeof(primes[0]); + for (int i = len - 1; i >= 0; i--) { + if (primes[i] < n) { + return primes[i]; + } + } + return 1; // 如果n小于2,返回1 +} + +typedef struct List +{ + int value; + struct List *next; +}List; + +typedef struct ListMap +{ + int hash; + int valid; + int value; + List *next; +}ListMap; + +ListMap *createListMap(int n){ + int size = getPri(n); + + ListMap *map = (ListMap *)malloc(sizeof(ListMap)*size); + if (map == NULL) { + printf("Error: Memory allocation failed\n"); + return NULL; + } + + // 初始化每个链表节点 + for (int i = 0; i < size; i++) { + map[i].hash = size; + map[i].valid = 0; // 初始状态 + map[i].value = 0; // 初始值 + map[i].next = NULL; // 初始指针 + } + + return map; +} + +int hashCode(int key, int hash) { + return key % hash; +} + +static int get(ListMap *map, int key) { + int hash = hashCode(key, map[0].hash); + if(map[hash].valid == 0){ + return -1; + }else{ + if(map[hash].value == key){ + return hash; + } + List *node = map[hash].next; + while (node) + { + if(node->value == key){ + return hash; + } + node = node->next; + } + } + return -1; +} + +int insert(ListMap *map, int key) { + int hash = hashCode(key, map[0].hash); + if(get(map, key) != -1){ + return 2; + } + if(map[hash].valid == 0){ + map[hash].valid = 1; + map[hash].value = key; + }else{ + List *node = map[hash].next; + while (node) + { + node = node->next; + } + + List *newNode = (List *)malloc(sizeof(List)); + newNode->value = key; + newNode->next = NULL; + node->next = newNode; + } + return 1; +} + +#endif diff --git a/we/23-1.h b/we/23-1.h new file mode 100644 index 0000000..d448935 --- /dev/null +++ b/we/23-1.h @@ -0,0 +1,16 @@ +#include + +int countSubstring(char *A,char *B){ + int count = 0; + char *p = A; + while(*p != '\0'){ + p = strstr(p,B); + if(p != NULL){ + count++; + p += strlen(B); + }else{ + break; + } + } + return count; +} \ No newline at end of file diff --git a/we/23-2.h b/we/23-2.h new file mode 100644 index 0000000..9a9240f --- /dev/null +++ b/we/23-2.h @@ -0,0 +1,60 @@ +#include +#include +#include + +struct ListNode { + char *val; + int count; + struct ListNode *next; +}; + +struct ListNode *initNode(char *val) { + struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); + node->val = val; + node->next = NULL; + return node; +} + +// 返回值带头节点 +struct ListNode *process(const char *fileName){ + struct ListNode *head = initNode(""); + + FILE *fp = fopen(fileName, "r"); + if(fp == NULL) { + printf("open file failed\n"); + return head; + } + + char buffer[1024]; + char *words[1000]; + int wordCount = 0; + + while (fscanf(fp, "%s", buffer) != EOF && wordCount < 1000) { + words[wordCount] = strdup(buffer); // 使用 strdup 复制字符串 + wordCount++; + } + + int tag = 0; // 0 表示还没有处理,1 表示已经处理 + + for (int i = 0; i < wordCount; i++) { + struct ListNode *p = head; + tag = 0; + while(p->next != NULL) { + if(strcmp(p->next->val, words[i]) == 0){ + p->next->count++; + tag = 1; + break; + } + p = p->next; + } + if(tag == 0) { + struct ListNode *node = initNode(words[i]); + node->count = 1; + p->next = node; + } + } + + fclose(fp); + + return head; +} \ No newline at end of file diff --git a/we/23-3.c b/we/23-3.c new file mode 100644 index 0000000..22b31f4 --- /dev/null +++ b/we/23-3.c @@ -0,0 +1,86 @@ +#include +#include + +struct student{ + int stuId; + char *name; + int score; +}; + +void inputData(struct student *s, int size){ + for(int i = 0; i < size; i++){ + printf("请输入第%d个学生的学号、姓名、成绩:", (i + 1)); + printf("学生学号:"); + scanf("%d", &s[i].stuId); + s[i].name = (char *)malloc(50 * sizeof(char)); + if (s[i].name == NULL) + { + printf("内存分配失败\n"); + } + + printf("学生姓名:"); + scanf("%s", s[i].name); + printf("学生成绩:"); + scanf("%d", &s[i].score); + } +} + +void bubbleSort(struct student *s, int size){ + for(int i = 0;i s[j+1].score){ + struct student temp = s[j]; + s[j] = s[j+1]; + s[j+1] = temp; + } + } + } +} + +struct student *findScore(struct student *s, int size, int score){ + int left = 0; + int right = size - 1; + int mid; + while(left <= right){ + mid = left + (right - left) / 2; + if(s[mid].score == score){ + return &s[mid]; + }else if (s[mid].score > score){ + right = mid - 1; + }else + { + left = mid + 1; + } + } + return NULL; +} + +int main(){ + int size; + printf("请输入学生人数:"); + scanf("%d",&size); + struct student *s = (struct student *)malloc(sizeof(struct student)*size); + inputData(s, size); + bubbleSort(s, size); + for(int i = 0;istuId,p->name,p->score); + } + + for (int i = 0; i < size; i++) + { + free[s[i].name]; + } + + free(s); + + return 0; +} \ No newline at end of file diff --git a/we/23-4.c b/we/23-4.c new file mode 100644 index 0000000..07d7543 --- /dev/null +++ b/we/23-4.c @@ -0,0 +1,73 @@ +#include +#include + +struct Toy +{ + int val; // 玩具的编号 + struct Toy *next; +}; + +void print(struct Toy *head){ + struct Toy *temp = head->next; + while(temp != NULL){ + printf("%d-",temp->val); + temp = temp->next; + } + printf("\n"); +} + +struct Toy *createToy(int val){ + struct Toy *t = (struct Toy *)malloc(sizeof(struct Toy)); + if(t == NULL){ + return NULL; + } + t->val = val; + return t; +} + +bool init(struct Toy *head){ + struct Toy *temp = head; + for(int i = 1;i<=10;i++){ + temp->next = createToy(i); + temp = temp->next; + if(temp == NULL){ + return false; + } + } + return true; +} + +void game(struct Toy *head,int val){ + struct Toy *temp = head; + while (temp->next != NULL) + { + if(temp->next->val == val){ + break; + } + temp = temp->next; + } + + struct Toy *t = temp->next; + temp->next = t->next; + t->next = head->next; + head->next = t; +} + +int main(){ + struct Toy *head = createToy(-1); //头节点 + if(head == NULL){ + printf("malloc fail\n"); + return -1; + } + bool in = init(head); + if(in == false){ + printf("init fail\n"); + return -1; + } + + print(head); + + game(head,3); // 骰子数为3 + print(head); + return 0; +} \ No newline at end of file diff --git a/we/24-1.h b/we/24-1.h new file mode 100644 index 0000000..2be6ab4 --- /dev/null +++ b/we/24-1.h @@ -0,0 +1,26 @@ +#include + +int getN(long int a){ + int ret = 0; + while (a) + { + ret++; + a/=10; + } + return ret; +} + +long int findMO(long int a){ + int ret = 0; + int i = 0; + int n = getN(a); + while(a!=0){ + int div = pow(10,n--); + int temp = a/div; + a%=div; + if(temp%2 == 1){ + ret += temp*(int)pow(10,i++); + } + } + return ret; +} \ No newline at end of file diff --git a/we/24-2.h b/we/24-2.h new file mode 100644 index 0000000..a0aeec7 --- /dev/null +++ b/we/24-2.h @@ -0,0 +1,33 @@ +#include +#include + +char* destar(char *str){ + int length = strlen(str); + char *ret = (char *)malloc(sizeof(char)*length); + if(ret == NULL){ + return "error"; + } + int left=0,right=length-1; + while (str[left++]=='*'); + while (str[right--]=='*'); + strncpy(ret,str,left); + char temp; + int i = left; + while (left<=right) + { + temp = str[left++]; + if((temp >= 'a' && temp <= 'z') || + (temp >= 'A' && temp <= 'Z')) { + ret[i++] = temp; + }else if(temp=='*'){ + continue; + }else{ + free(ret); + return "error"; + } + } + strncpy(ret+i,str+right+1,length - (right + 1)); + ret[length - (left - i)] = '\0'; + + return ret; +} \ No newline at end of file diff --git a/we/24-3.h b/we/24-3.h new file mode 100644 index 0000000..67cd478 --- /dev/null +++ b/we/24-3.h @@ -0,0 +1,25 @@ +struct List{ + int val; + struct List * next; +}; + +void bubbleListSort(List *head) { + if (head == nullptr) return; // 空链表检查 + + List *temp2; + int temp; + bool swapped; + do { + swapped = false; + temp2 = head; + while (temp2->next) { + if (temp2->next->val < temp2->val) { + temp = temp2->val; + temp2->val = temp2->next->val; + temp2->next->val = temp; + swapped = true; + } + temp2 = temp2->next; + } + } while (swapped); // 如果没有发生交换,提前结束 +} \ No newline at end of file diff --git a/we/24-4.h b/we/24-4.h new file mode 100644 index 0000000..d3cc2ff --- /dev/null +++ b/we/24-4.h @@ -0,0 +1,114 @@ +#include +#include +#include + +int find(char **species,int size,char *str){ + for(int i = 0; i cost){ + min = cost; + res = i; + } + } + + // 释放内存 + for(int i = 0; i < size; i++){ + free(matrix[i]); + } + free(matrix); + + char *animal = res != -1 ? species[res] : NULL; + + free(species); + + return animal; +} \ No newline at end of file diff --git a/we/24-sy-1.c b/we/24-sy-1.c new file mode 100644 index 0000000..09ff2e9 --- /dev/null +++ b/we/24-sy-1.c @@ -0,0 +1,14 @@ +#include + +bool isPalindrome(char *s){ + int len = strlen(s); + int left = 0, right = len - 1; + while (left < right) { + if (s[left] != s[right]) { + return false; + } + left++; + right--; + } + return true; +} \ No newline at end of file diff --git a/we/24-sy-2.c b/we/24-sy-2.c new file mode 100644 index 0000000..e75fa65 --- /dev/null +++ b/we/24-sy-2.c @@ -0,0 +1,36 @@ +#include + +struct ListNode +{ + int val; + struct ListNode *next; +}; + + +// 带头节点的单链表插入排序 +void insertSort(struct ListNode *head){ + struct ListNode *p = head->next; + struct ListNode *q; + + if (p == NULL || p->next == NULL) return; // 空链表或单节点无需排序 + + while(p->next) { + q = head; + while (q->next != p->next) { + if(q->next->val < p->next->val) { + q = q->next; + } else { + // 插入操作 + struct ListNode *temp = p->next; + p->next = temp->next; + temp->next = q->next; + q->next = temp; + break; // 插入完成后立即跳出 + } + } + // 只有没发生插入时才移动p指针 + if (q->next == p->next) { + p = p->next; + } + } +} diff --git a/we/24-sy-3.c b/we/24-sy-3.c new file mode 100644 index 0000000..d8e39e8 --- /dev/null +++ b/we/24-sy-3.c @@ -0,0 +1,18 @@ +#include +#include + +char *strcpys(char *dest,char *src){ + int len1 = strlen(dest); + int len2 = strlen(src); + char *res = (char *)malloc(sizeof(char)*(len1+len2+1)); + if(!res){ + printf("malloc failed\n"); + return NULL; + } + memcpy(res,dest,len1); + memcpy(res+len1,src,len2); + res[len1+len2] = '\0'; + + free(src); + return res; +} \ No newline at end of file