This commit is contained in:
2025-09-15 21:12:04 +08:00
commit 3f58f483ff
144 changed files with 5298 additions and 0 deletions

50
23/04/1019.c Normal file
View File

@@ -0,0 +1,50 @@
struct ListNode
{
int val;
struct ListNode *next;
};
typedef struct Pair {
int first;
int second;
} Pair;
int getLength(struct ListNode* head){
int len = 0;
ListNode* node = head;
while (node.next)
{
node = node.next;
len++;
}
return len;
}
int* nextLargerNodes(struct ListNode* head, int* returnSize){
int len = 0;
struct ListNode* cur = head;
while (cur) {
cur = cur->next;
len++;
}
int* ans = (int *)calloc(len, sizeof(int));
Pair stack[len];
int top = 0, pos = 0;
cur = head;
int idx = -1;
while (cur) {
++idx;
ans[pos++] = 0;
while (top > 0 && stack[top - 1].first < cur->val) {
ans[stack[top - 1].second] = cur->val;
top--;
}
stack[top].first = cur->val;
stack[top].second = idx;
top++;
cur = cur->next;
}
*returnSize = len;
return ans;
}

20
23/04/2399.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdbool.h>
#include <string.h>
bool checkDistances(char *s, int *distance, int distanceSize)
{
for (int i = 0; i < strlen(s); i++)
{
int site = s[i] - 'a';
if (distance[i] == -1)
{
continue;
}
if (site + distance[i] + 1 >= strlen(s) || s[i] != s[i + distance[site] + 1])
{
return false;
}
distance[i] = -1;
}
return true;
}

57
23/05/1373.c Normal file
View File

@@ -0,0 +1,57 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
const int INF = 0x3f3f3f3f;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
typedef struct SubTree {
bool isBST;
int minValue;
int maxValue;
int sumValue;
} SubTree;
SubTree *createSubTree(bool isBST, int minValue,int maxValue, int sumValue) {
SubTree *obj = (SubTree *)malloc(sizeof(SubTree));
obj->isBST = isBST;
obj->minValue = minValue;
obj->maxValue = maxValue;
obj->sumValue = sumValue;
return obj;
}
SubTree* dfs(struct TreeNode* root, int *res) {
if (root == NULL) {
return createSubTree(true, INF, -INF, 0);
}
SubTree *left = dfs(root->left, res);
SubTree *right = dfs(root->right, res);
SubTree *ret = NULL;
if (left->isBST && right->isBST &&
root->val > left->maxValue &&
root->val < right->minValue) {
int sum = root->val + left->sumValue + right->sumValue;
*res = MAX(*res, sum);
ret = createSubTree(true, MIN(left->minValue, root->val), \
MAX(root->val, right->maxValue), sum);
} else {
ret = createSubTree(false, 0, 0, 0);
}
free(left);
free(right);
return ret;
}
int maxSumBST(struct TreeNode* root){
int res = 0;
SubTree *obj = dfs(root, &res);
free(obj);
return res;
}

26
23/05/33.c Normal file
View File

@@ -0,0 +1,26 @@
static int max(int a, int b) {
return a > b ? a : b;
}
static int min(int a, int b) {
return a < b ? a : b;
}
int storeWater(int* bucket, int bucketSize, int* vat, int vatSize) {
int maxk = 0;
for (int i = 0; i < vatSize; i++) {
maxk = max(maxk, vat[i]);
}
if (maxk == 0) {
return 0;
}
int res = INT_MAX;
for (int k = 1; k <= maxk && k < res; ++k) {
int t = 0;
for (int i = 0; i < bucketSize; ++i) {
t += max(0, (vat[i] + k - 1) / k - bucket[i]);
}
res = min(res, t + k);
}
return res;
}

22
23/07/2208.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <vector>
#include <queue>
#include <numeric>
using namespace std;
class Solution {
public:
int halveArray(vector<int>& nums) {
priority_queue<double> pq(nums.begin(), nums.end());
int res = 0;
double sum = accumulate(nums.begin(), nums.end(), 0.0), sum2 = 0.0;
while (sum2 < sum / 2) {
double x = pq.top();
pq.pop();
sum2 += x / 2;
pq.push(x / 2);
res++;
}
return res;
}
};

22
23/07/2500.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
int deleteGreatestValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
for (int i = 0; i < m; i++) {
sort(grid[i].begin(), grid[i].end());
}
int res = 0;
for (int j = 0; j < n; j++) {
int mx = 0;
for (int i = 0; i < m; i++) {
mx = max(mx, grid[i][j]);
}
res += mx;
}
return res;
}
};

55
23/07/771.c Normal file
View File

@@ -0,0 +1,55 @@
#include "math.h"
#include "stdio.h"
// 未验证leecode使用了 uthash.h 库进行处理
typedef struct linked {
char val;
struct linked *next;
} *HashTable;
HashTable *createTable(int hashCode, const char *jewels) {
HashTable table[hashCode];
for (int i = 0; i < sizeof(jewels) / sizeof(jewels[0]); ++i) {
int temp = jewels[i] % hashCode;
struct linked link = {jewels[i], NULL};
if (table[temp] == NULL) {
table[temp] = &link;
} else {
HashTable temps = table[temp];
if (temps->val == jewels[i]) {
continue;
}
while (temps->next != NULL) {
temps = temps->next;
if (temps->val == jewels[i]) {
continue;
}
}
temps->next = &link;
}
}
return table;
}
int contained(char x, HashTable *table, int hashCode) {
int temp = x % hashCode;
HashTable temps = table[temp];
while (temps){
if (temps->val == x){
return 1;
}
}
return 0;
}
int numJewelsInStones(char *jewels, char *stones) {
int count = 0;
int hashCode = (int) sqrt(sizeof(jewels) / sizeof(jewels[0])) + 1;
HashTable *table = createTable(hashCode, jewels);
for (int i = 0; i < sizeof(stones) / sizeof(stones[0]); ++i) {
if (contained(stones[i], table, hashCode)) {
count++;
}
}
return count;
}

14
23/08/1281.cpp Normal file
View File

@@ -0,0 +1,14 @@
class Q1281 {
public:
int subtractProductAndSum(int n) {
int plus = 0;
int mult = 1;
while (n) {
int quo = n % 10;
n = n / 10;
plus += quo;
mult *= quo;
}
return mult - plus;
}
};

9
23/08/1749.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "vector"
using namespace std;
class Q1749 {
public:
int maxAbsoluteSum(vector<int>& nums) {
}
};

38
23/08/21.cpp Normal file
View File

@@ -0,0 +1,38 @@
class Q21
{
public:
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
ListNode *preHead = new ListNode(-1);
ListNode *prev = preHead;
while (l1 != nullptr && l2 != nullptr)
{
if (l1->val < l2->val)
{
prev->next = l1;
l1 = l1->next;
}
else
{
prev->next = l2;
l2 = l2->next;
}
prev = prev->next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev->next = l1 == nullptr ? l2 : l1;
std::cout << "123";
return preHead->next;
}
};

28
23/08/24.cpp Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include "lists.h"
class Q24 {
public:
ListNode *swapPairs(ListNode *head) {
if (!head || !head->next) {
return head;
}
ListNode *p1;
ListNode *p2;
p1 = head;
p2 = head->next;
while (!p2){
p1->next = p2->next;
p2->next = p1;
if(!p1->next){
return head;
}
p1 = p1->next;
p2 = p1->next;
}
return head;
}
};

13
23/08/Q1388.cpp Normal file
View File

@@ -0,0 +1,13 @@
//
// Created by 李洋 on 2023/8/18.
//
#include <vector>
using namespace std;
class Q1388 {
public:
int maxSizeSlices(vector<int> &slices) {
}
};

17
23/08/Q1572.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include <vector>
using namespace std;
class Q1572 {
public:
int diagonalSum_1(vector<vector<int>> &mat) {
int sum = 0;
for (int i = 0; i < mat.size(); ++i) {
sum += mat[i][i] + mat[i][mat.size() - i - 1];
}
if ((mat.size() & 1) == 1) {
sum -= mat[mat.size() / 2][mat.size() / 2];
}
return sum;
}
};

14
23/08/Q2235.cpp Normal file
View File

@@ -0,0 +1,14 @@
//
// Created by 李洋 on 2023/8/19.
//
class Q2235 {
public:
int sum(int num1, int num2) {
while (num2) {
unsigned int carry = (unsigned int) (num1 & num2) << 1;
num1 ^= num2;
num2 = carry;
}
return num1;
}
};

15
23/08/Q23.cpp Normal file
View File

@@ -0,0 +1,15 @@
//
// Created by 李洋 on 2023/8/12.
//
#include "../../dataStruct/LinkedList/lists.h"
using namespace std;
class Q23 {
public:
bool compare(const ListNode *a, const ListNode *b) {
return a->val < b->val;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
}
};

22
23/08/Q2682.cpp Normal file
View File

@@ -0,0 +1,22 @@
//
// Created by 李洋 on 2023/8/16.
//
#include "vector"
using namespace std;
class Q2682 {
public:
vector<int> circularGameLosers(int n, int k) {
vector<bool> visit(n, false);
for (int i = k, j = 0; !visit[j]; i += k) {
visit[j] = true;
j = (j + i) % n;
}
vector<int> ans;
for (int i = 0; i < n; i++) {
if (!visit[i]) {
ans.emplace_back(i + 1);
}
}
return ans;
}
};

20
23/08/Q617.cpp Normal file
View File

@@ -0,0 +1,20 @@
//
// Created by 李洋 on 2023/8/14.
//
#include "../../dataStruct/Tree/Tree.h"
class Q617 {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr) {
return root2;
}
if (root2 == nullptr) {
return root1;
}
auto merged = new TreeNode(root1->val + root2->val);
merged->left = mergeTrees(root1->left, root2->left);
merged->right = mergeTrees(root1->right, root2->right);
return merged;
}
};

28
23/08/Q88.cpp Normal file
View File

@@ -0,0 +1,28 @@
//
// Created by 李洋 on 2023/8/13.
//
#include <vector>
using namespace std;
class Q88 {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int p1 = 0, p2 = 0;
int sorted[m + n];
int cur;
while (p1 < m || p2 < n) {
if (p1 == m) {
cur = nums2[p2++];
} else if (p2 == n) {
cur = nums1[p1++];
} else if (nums1[p1] < nums2[p2]) {
cur = nums1[p1++];
} else {
cur = nums2[p2++];
}
sorted[p1 + p2 - 1] = cur;
}
for (int i = 0; i != m + n; ++i) {
nums1[i] = sorted[i];
}
}
};

16
23/09/2594.h Normal file
View File

@@ -0,0 +1,16 @@
//
// Created by 李洋 on 2023/9/7.
//
#ifndef LEECODE_C_2594_H
#define LEECODE_C_2594_H
#include <vector>
using namespace std;
long long repairCars(vector<int> &ranks, int cars) {
}
#endif //LEECODE_C_2594_H

34
23/09/Q1123.h Normal file
View File

@@ -0,0 +1,34 @@
//
// Created by 李洋 on 2023/9/6.
//
#ifndef LEECODE_C_Q1123_H
#define LEECODE_C_Q1123_H
#pragma once
#include "../../dataStruct/Tree/TreeStack.h"
std::pair<TreeNode *, int> f(TreeNode *root) {
if (!root) {
return {root, 0};
}
auto left = f(root->left);
auto right = f(root->right);
if (left.second > right.second) {
return {left.first, left.second + 1};
}
if (left.second < right.second) {
return {right.first, right.second + 1};
}
return {root, left.second + 1};
}
struct TreeNode *lcaDeepestLeaves(struct TreeNode *root) {
return f(root).first;
}
#endif //LEECODE_C_Q1123_H

21
23/09/Q2240.h Normal file
View File

@@ -0,0 +1,21 @@
//
// Created by 李洋 on 2023/9/1.
//
#ifndef LEECODE_C_2240_H
#define LEECODE_C_2240_H
class Q2240 {
public:
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
long long count = total / cost2 + 1;
int tos = total / cost1;
for (int i = 1; i <= tos; ++i) {
int temp = total - i * cost1;
count += temp / cost2 + 1;
}
return count;
}
};
#endif //LEECODE_C_2240_H

26
23/10/2520.h Normal file
View File

@@ -0,0 +1,26 @@
//
// Created by 李洋 on 2023/10/26.
//
#ifndef LEECODE_C_2520_H
#define LEECODE_C_2520_H
#include <string>
using namespace std;
class Q2520 {
public:
int countDigits(int num) {
string str = to_string(num);
int res = 0;
for (auto &ch: str) {
if (num % (ch - '0') == 0) {
res++;
}
}
return res;
}
};
#endif //LEECODE_C_2520_H

49
23/10/901.h Normal file
View File

@@ -0,0 +1,49 @@
//
// Created by 李洋 on 2023/10/7.
//
#ifndef LEECODE_C_Q901_H
#define LEECODE_C_Q901_H
#include <stack>
using namespace std;
class trendSpanner {
public:
trendSpanner() {}
int next(int price) {
count = 1;
if (trend.size() == 0 || price < trend.top()->value) {
trend.push(new stock{price, 1});
return count;
}
while (!trend.empty() && price >= trend.top()->value) {
count += trend.top()->value;
stock *temp = trend.top();
trend.pop();
delete (temp);
}
trend.push(new stock{price, count});
return count;
}
private:
struct stock {
int key;
int value;
};
stack<stock *> trend;
int count;
};
/**
* Your trendSpanner object will be instantiated and called as such:
* trendSpanner* obj = new trendSpanner();
* int param_1 = obj->next(price);
*/
#endif LEECODE_C_Q901_H

25
23/10/Q1346.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef LEECODE_C_Q1346_H
#define LEECODE_C_Q1346_H
#include <map>
using namespace std;
class Q1346 {
public:
bool checkIfExist(vector<int>& arr) {
unordered_map<int,int> m;
for(auto i:arr){
m[i] = 1;
}
for(auto [key,value]:m){
if (m[key*2] == 1)
{
return true;
}
}
return false;
}
};
#endif LEECODE_C_Q1346_H

23
23/10/Q136.h Normal file
View File

@@ -0,0 +1,23 @@
//
// Created by 李洋 on 2023/10/14.
//
#ifndef LEECODE_C_Q136_H
#define LEECODE_C_Q136_H
#include <vector>
using namespace std;
class Q136 {
public:
int singleNumber(vector<int> &nums) {
int count = 0;
for (int i: nums) {
count ^= i;
}
return count;
}
};
#endif //LEECODE_C_Q136_H

29
23/10/Q137.h Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by 李洋 on 2023/10/16.
//
#ifndef LEECODE_C_Q137_H
#define LEECODE_C_Q137_H
#include <vector>
#include <map>
using namespace std;
class Q137 {
public:
int singleNumber(vector<int> &nums) {
unordered_map<int, int> m;
for (int i: nums) {
m[i] = m[i] + 1;
}
for (auto [num, count]: m) {
if (count == 1) {
return num;
}
}
return -1;
}
};
#endif //LEECODE_C_Q137_H

41
23/10/Q1465.h Normal file
View File

@@ -0,0 +1,41 @@
//
// Created by 李洋 on 2023/10/27.
//
#ifndef LEECODE_C_Q1465_H
#define LEECODE_C_Q1465_H
#include <vector>
using namespace std;
class Q1465 {
public:
int maxArea(int h, int w, vector<int> &horizontalCuts, vector<int> &verticalCuts) {
int maxHorizon;
int maxVertical;
if (horizontalCuts.size()) {
maxHorizon = max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.size() - 1]);
} else {
maxHorizon = h;
}
if (verticalCuts.size()) {
maxVertical = max(verticalCuts[0], w - verticalCuts[verticalCuts.size() - 1]);
} else {
maxVertical = w;
}
for (int i = 1; i < horizontalCuts.size(); ++i) {
maxHorizon = max(maxHorizon, horizontalCuts[i] - horizontalCuts[i - 1]);
}
for (int i = 1; i < verticalCuts.size(); ++i) {
maxVertical = max(maxVertical, verticalCuts[i] - verticalCuts[i - 1]);
}
return (long long) maxHorizon * maxVertical % 1000000001;
}
};
#endif //LEECODE_C_Q1465_H

44
23/10/Q1488.h Normal file
View File

@@ -0,0 +1,44 @@
//
// Created by 李洋 on 2023/10/13.
//
#ifndef LEECODE_C_Q1488_H
#define LEECODE_C_Q1488_H
#include <vector>
#include <map>
using namespace std;
class Q1488 {
public:
vector<int> avoidFlood(vector<int> &rains) {
map<int, int> m;
vector<int> ret;
for (int i = 0; i < rains.size(); i++) {
if (rains[i] == 0) {
for (int j = i + 1; j < rains.size(); ++j) {
if (j > 0) {
if (m[rains[j]] == 1) {
m.erase(rains[j]);
ret.push_back(rains[j]);
break;
}
}
if (j == rains.size() - 1) {
ret.push_back(1);
}
}
continue;
}
if (m[rains[i]]) {
return vector<int>();
}
m[rains[i]] = 1;
ret.push_back(-1);
}
return ret;
}
};
#endif //LEECODE_C_Q1488_H

77
23/10/Q1726.h Normal file
View File

@@ -0,0 +1,77 @@
//
// Created by 李洋 on 2023/10/19.
//
#ifndef LEECODE_C_Q1726_H
#define LEECODE_C_Q1726_H
#include <vector>
#include <map>
#include <../../dataStruct/Combination.h>
using namespace std;
int tupleSameProduct(vector<int> &nums) {
int count = 0;
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
m[nums[i] * nums[j]] = m[nums[i] * nums[j]] + 1;
}
}
for (auto &pair: m) {
if (pair.second > 1) {
count += pair.second * (pair.second - 1) * 4;
}
}
return count;
}
int tupleSameProduct2(vector<int> &nums) {
int count = 0;
vector<int> result;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
result.push_back(nums[i] * nums[j]);
}
}
sort(result.begin(), result.end());
int temp = 0;
int num = nums[0];
for (int i = 1; i < result.size(); ++i) {
if (result[i] == num) {
count += (temp++) * 8;
} else {
num = result[i];
temp = 0;
}
if (result[i] == result[i - 1]) {
count += 8;
}
}
return count;
}
int tupleSameProduct3(vector<int> &nums) {
int count = 0;
vector<int> result;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
result.push_back(nums[i] * nums[j]);
}
}
sort(result.begin(), result.end());
int temp = 1;
int num = nums[0];
for (int i = 1; i < result.size(); ++i) {
if (result[i] == num) {
temp++;
} else {
count += temp * (temp - 1) * 4;
num = result[i];
temp = 1;
}
}
return count;
}
#endif //LEECODE_C_Q1726_H

29
23/10/Q2525.h Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by 李洋 on 2023/10/20.
//
#ifndef LEECODE_C_Q2525_H
#define LEECODE_C_Q2525_H
#include <string>
using namespace std;
class Q2525 {
public:
string categorizeBox(int length, int width, int height, int mass) {
if (length >= 10000 || width >= 10000 || height >= 10000 || (long long)length * width * height >= 1000000000) {
if (mass >= 100) {
return "Both";
} else {
return "Bulky";
}
}
if (mass >= 100) {
return "Heavy";
}
return "Neither";
}
};
#endif //LEECODE_C_Q2525_H

33
23/10/Q2526.h Normal file
View File

@@ -0,0 +1,33 @@
//
// Created by 李洋 on 2023/10/12.
//
#ifndef LEECODE_C_Q2526_H
#define LEECODE_C_Q2526_H
#include <vector>
using namespace std;
class Q2526 {
public:
long long findTheArrayConcVal(vector<int> &nums) {
long long ans = 0;
for (int i = 0, j = nums.size() - 1; i <= j; i++, j--) {
if (i != j) {
int temp = nums[j];
int digit = 0;
while (temp != 0) {
digit++;
temp /= 10;
}
ans += nums[j] + (long long) nums[i] * pow(10, digit);
} else {
ans += nums[i];
}
}
return ans;
}
};
#endif //LEECODE_C_Q2526_H

26
23/10/Q2530.h Normal file
View File

@@ -0,0 +1,26 @@
//
// Created by 李洋 on 2023/10/18.
//
#ifndef LEECODE_C_Q2530_H
#define LEECODE_C_Q2530_H
#include <vector>
#include <queue>
using namespace std;
long long maxKelements(vector<int> &nums, int k) {
long long count = 0;
priority_queue<int> Q(nums.begin(), nums.end());
while (k) {
auto top = Q.top();
Q.pop();
count += top;
Q.push(ceil(top / 3.0));
k--;
}
return count;
}
#endif //LEECODE_C_Q2530_H

35
23/10/Q260.h Normal file
View File

@@ -0,0 +1,35 @@
//
// Created by 李洋 on 2023/10/16.
//
#ifndef LEECODE_C_Q260_H
#define LEECODE_C_Q260_H
#include <vector>
#include <map>
using namespace std;
class Q260 {
public:
vector<int> singleNumber(vector<int> &nums) {
unordered_map<int, int> m;
for (int i: nums) {
m[i] = m[i] + 1;
}
vector<int> ret;
for (auto [num, count]: m) {
if (count == 1) {
if (ret.empty()) {
ret.push_back(num);
} else {
ret.push_back(num);
return ret;
}
}
}
return ret;
}
};
#endif //LEECODE_C_Q260_H

19
23/10/Q2652.h Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by 李洋 on 2023/10/17.
//
#ifndef LEECODE_C_Q2652_H
#define LEECODE_C_Q2652_H
class Q2652 {
public:
int f(int n, int m) {
return (m + n / m * m) * (n / m) / 2;
}
int sumOfMultiples(int n) {
return f(n, 3) + f(n, 5) + f(n, 7) - f(n, 3 * 5) - f(n, 3 * 7) - f(n, 5 * 7) + f(n, 3 * 5 * 7);
}
};
#endif //LEECODE_C_Q2652_H

26
23/10/Q2698.h Normal file
View File

@@ -0,0 +1,26 @@
//
// Created by 李洋 on 2023/10/25.
//
#ifndef LEECODE_C_Q2698_H
#define LEECODE_C_Q2698_H
#include <vector>
using namespace std;
class Q2698 {
public:
int punishmentNumber(int n) {
auto all = vector<int>(
{1, 9, 10, 36, 45, 55, 82, 91, 99, 100, 235, 297, 369, 370, 379, 414, 657, 675, 703, 756, 792, 909, 918,
945, 964, 990, 991, 999, 1000});
int count = 0;
for (int i = 0; i < all.size() && all[i] <= n; ++i) {
count += all[i] * all[i];
}
return count;
}
};
#endif //LEECODE_C_Q2698_H

30
23/10/Q274.h Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by 李洋 on 2023/10/29.
//
#ifndef LEECODE_C_Q274_H
#define LEECODE_C_Q274_H
#include <vector>
using namespace std;
class Q274 {
public:
int hIndex(vector<int> &citations) {
sort(citations.begin(), citations.end(), [](int a, int b) {
return a > b;
});
for (int i = 1; i < citations.size(); ++i) {
if (citations[i - 1] < i) {
return i - 1;
}
if (i + 1 == citations.size() - 1) {
return i + 1;
}
}
return citations[0] == 0 ? 0 : 1;
}
};
#endif //LEECODE_C_Q274_H

29
23/10/Q275.h Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by 李洋 on 2023/10/30.
//
#ifndef LEECODE_C_Q275_H
#define LEECODE_C_Q275_H
#include <vector>
using namespace std;
class Q275 {
public:
int hIndex(vector<int> &citations) {
int n = citations.size();
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (citations[mid] >= n - mid) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return n - left;
}
};
#endif //LEECODE_C_Q275_H

57
23/10/T368.h Normal file
View File

@@ -0,0 +1,57 @@
//
// Created by 李洋 on 2023/10/22.
//
#ifndef LEECODE_C_T368_H
#define LEECODE_C_T368_H
#include <vector>
using namespace std;
int minimumSum(vector<int> &nums) {
int left = 0;
int right = nums.size() - 1;
for (int i = 0; i < right; ++i) {
if (nums[left] >= nums[i]) {
left = i;
}
}
for (int i = right; i > left; --i) {
if (nums[right] >= nums[i]) {
right = i;
}
}
if (right - left == 1) {
return -1;
}
int max = left + 1;
for (int i = left + 1; i < right; ++i) {
if (nums[max] >= nums[i]) {
max = i;
}
}
return nums[left] + nums[right] + nums[max];
}
void runT368() {
vector<int> arr; // [5,4,8,7,10,2] / [6,5,4,3,4,5]
/* arr.push_back(5);
arr.push_back(4);
arr.push_back(8);
arr.push_back(7);
arr.push_back(10);
arr.push_back(2);*/
// arr.push_back(6);
// arr.push_back(5);
// arr.push_back(4);
// arr.push_back(3);
// arr.push_back(4);
// arr.push_back(5);
arr.push_back(50);
arr.push_back(50);
arr.push_back(50);
auto result = minimumSum(arr);
}
#endif //LEECODE_C_T368_H

50
23/11/Q117.h Normal file
View File

@@ -0,0 +1,50 @@
//
// Created by 李洋 on 2023/11/3.
//
#ifndef LEECODE_C_Q117_H
#define LEECODE_C_Q117_H
#include <stack>
#include <queue>
using namespace std;
class Node {
public:
int val;
Node *left;
Node *right;
Node *next;
Node() : val(0), left(nullptr), right(nullptr), next(nullptr) {}
Node(int _val) : val(_val), left(nullptr), right(nullptr), next(nullptr) {}
Node(int _val, Node *_left, Node *_right, Node *_next)
: val(_val), left(_left), right(_right), next(_next) {}
};
Node *connect(Node *root) {
if (!root) {
return nullptr;
}
queue<pair<Node *, int>> Q;
Q.emplace(root, 1);
while (!Q.empty()) {
auto temp = Q.front();
Q.pop();
if (temp.second == Q.front().second) {
temp.first->next = Q.front().first;
}
if (temp.first->left != nullptr) {
Q.emplace(temp.first->left, temp.second + 1);
}
if (temp.first->right != nullptr) {
Q.emplace(temp.first->right, temp.second + 1);
}
}
return root;
}
#endif //LEECODE_C_Q117_H

28
23/11/Q187.h Normal file
View File

@@ -0,0 +1,28 @@
//
// Created by 李洋 on 2023/11/5.
//
#ifndef LEECODE_C_Q187_H
#define LEECODE_C_Q187_H
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
vector<string> findRepeatedDnaSequences(string s) {
vector<string> ret;
unordered_map<string, int> m;
for(int i = 0;i+9<s.length();i++){
m[s.substr(i,i+9)] = m[s.substr(i,i+9)] + 1;
}
for(auto [key,value]:m){
if(value > 1){
ret.push_back(key);
}
}
return ret;
}
#endif //LEECODE_C_Q187_H

28
23/11/Q2342.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef LEECODE_C_Q2342_H
#define LEECODE_C_Q2342_H
#include <vector>
#include <map>
using namespace std;
int maximumSum(vector<int> &nums)
{
int ans = -1;
int mx[82]{}; // 至多 9 个 9 相加
for (int num : nums)
{
int s = 0; // num 的数位和
for (int x = num; x; x /= 10)
{ // 枚举 num 的每个数位
s += x % 10;
}
if (mx[s])
{ // 说明左边也有数位和等于 s 的元素
ans = max(ans, mx[s] + num); // 更新答案的最大值
}
mx[s] = max(mx[s], num); // 维护数位和等于 s 的最大元素
}
return ans;
}
#endif LEECODE_C_Q2342_H

31
23/11/Q2586.h Normal file
View File

@@ -0,0 +1,31 @@
//
// Created by 李洋 on 2023/11/8.
//
#ifndef LEECODE_C_Q2586_H
#define LEECODE_C_Q2586_H
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
int vowelStrings(vector<string> &words, int left, int right) {
unordered_map<char, int> m;
m['a'] = 1;
m['e'] = 1;
m['i'] = 1;
m['o'] = 1;
m['u'] = 1;
int count = 0;
for (int i = left; i <= right; ++i) {
auto s = words[i];
if (s != "" && m[s[0]] && m[s[s.length() - 1]]) {
count++;
}
}
return count;
}
#endif //LEECODE_C_Q2586_H

25
23/11/Q2609.h Normal file
View File

@@ -0,0 +1,25 @@
//
// Created by 李洋 on 2023/11/8.
//
#ifndef LEECODE_C_Q2609_H
#define LEECODE_C_Q2609_H
#include <string>
#include <vector>
#include <stack>
using namespace std;
int findTheLongestBalancedSubstring(string s) {
int n = s.size(), idx = 0, ans = 0;
while (idx < n) {
int a = 0, b = 0;
while (idx < n && s[idx] == '0' && ++a >= 0) idx++;
while (idx < n && s[idx] == '1' && ++b >= 0) idx++;
ans = max(ans, min(a, b) * 2);
}
return ans;
}
#endif //LEECODE_C_Q2609_H

17
23/11/Q318.h Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by 李洋 on 2023/11/6.
//
#ifndef LEECODE_C_Q318_H
#define LEECODE_C_Q318_H
#include <string>
#include <vector>
using namespace std;
int maxProduct(vector<string> &words) {
}
#endif //LEECODE_C_Q318_H

33
23/11/Q876.h Normal file
View File

@@ -0,0 +1,33 @@
//
// Created by 李洋 on 2023/11/7.
//
#ifndef LEECODE_C_Q876_H
#define LEECODE_C_Q876_H
#include <vector>
#include <stack>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode *middleNode(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
#endif //LEECODE_C_Q876_H