mirror of
https://gitlab.com/game-loader/hugo.git
synced 2025-11-29 11:37:41 +08:00
docs(posts): add new content and LeetCode solutions
This commit is contained in:
@ -135,6 +135,10 @@ func main(){
|
||||
|
||||
由此, 数据库中保存用户id, 调用的生图模型, 生图的提示词和成功生成的图片url即可. 生成的图片保存时间有限, 因此要将图片上传到图床以长时间保存. 而对于用户,先返回生成图片的url即可, 后续再次访问时从数据库中取数据则是图床的url.
|
||||
|
||||
### 如何设计实时聊天的数据库
|
||||
|
||||
起初总想将群组聊天和个人私聊统一起来,实际没有必要,将群组聊天和个人私聊作为两种不同的消息类型分别进行处理即可,将个人私聊的消息放入一张表中,将群组聊天消息放入另一张表中。
|
||||
|
||||
## 使用airbyte进行mysql和mongodb的数据库同步
|
||||
|
||||
### 设置mariadb的log-bin和server-id
|
||||
@ -205,3 +209,9 @@ sudo setfacl -m u:libvirt-qemu:r /kvm/ubuntu_disk.qcow2
|
||||

|
||||
|
||||
并不是只返回这个数据对象,而是这个数据对象是在data中的数据,除此之外还默认会返回status, msg,sub这三个字段,就像post请求中一般的返回数据一样,除了这三个字段外还有data字段,这个字段会放Get请求中我们请求的目标数据。
|
||||
|
||||
## 阿里云相关问题
|
||||
|
||||
### RAM权限控制问题
|
||||
|
||||
注意使用sts token的时候看ARN的角色权限都有哪些,如果授权的范围特别小,比如角色权限仅限对特定bucket的读写权限,则当bucket发生变化如创建了新的bucket,bucket名字发生变化时尽管可以生成sts token,但将提示没有指定bucket的权限,即403,所以一定确认ARN角色的权限。
|
||||
|
||||
@ -23702,3 +23702,145 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
## day352 2025-02-25
|
||||
### 1524. Number of Sub-arrays With Odd Sum
|
||||
Given an array of integers arr, return the number of subarrays with an odd sum.
|
||||
|
||||
Since the answer can be very large, return it modulo 109 + 7.
|
||||
|
||||

|
||||
|
||||
### 题解
|
||||
|
||||
本题注意题目要求仅需要判断子数组和的奇偶性,并不要求得到子数组和的具体数值,因此要从奇偶性的特点出发,寻找可以简化解题过程的方法。
|
||||
|
||||
如果仅考虑计算得到子数组和的情况下,每次向后遍历新的数字时只需将新数字和以前一个数字为结尾的后缀和数组依次相加即得到新的子数组和,即当前数字下标为n+1的情况下,只需将数字分别和n,n+n-1,n+n-1+n-2...相加即得以n+1结尾的子数组和,但由于题目不要求得到子数组和的具体数值,只要求得到奇偶性,因此可以将计算具体的子数组和简化为仅分别记录以n+1结尾的子数组中子数组和为奇数和偶数的个数。根据奇数与奇数相加得到偶数,偶数与奇数相加得到奇数的规则,若n+1对应的数字为奇数,则将n对应的奇数和偶数个数互换并,将奇数个数再加一(加上n+1自身)作为新的奇数和偶数个数,对应的数字为偶数时处理方式类似。将总个数与当前奇数个数相加并模10^9+7。
|
||||
|
||||
### 代码
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
const int MOD = 1e9 + 7;
|
||||
int numOfSubarrays(vector<int>& arr) {
|
||||
int oddsum = 0;
|
||||
int currentodd = 0;
|
||||
int currenteven = 0;
|
||||
int temp = 0;
|
||||
for(const int& num:arr){
|
||||
if(num % 2 == 1){
|
||||
temp = currentodd;
|
||||
currentodd = currenteven + 1;
|
||||
currenteven = temp;
|
||||
}else{
|
||||
currenteven++;
|
||||
}
|
||||
oddsum = (oddsum + currentodd) % MOD;
|
||||
}
|
||||
return oddsum;
|
||||
}
|
||||
};
|
||||
```
|
||||
## day353 2025-02-26
|
||||
### 1749. Maximum Absolute Sum of Any Subarray
|
||||
You are given an integer array nums. The absolute sum of a subarray [numsl, numsl+1, ..., numsr-1, numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr).
|
||||
|
||||
Return the maximum absolute sum of any (possibly empty) subarray of nums.
|
||||
|
||||
Note that abs(x) is defined as follows:
|
||||
|
||||
If x is a negative integer, then abs(x) = -x.
|
||||
If x is a non-negative integer, then abs(x) = x.
|
||||
|
||||

|
||||
|
||||
### 题解
|
||||
昨天的题目要考虑问题的奇偶性,今天就要考虑子数组的和,此处注意要考虑的是子数组和的绝对值,这简化了这个问题,之前多次提到,任意子数组的和都可以通过两个前缀和相减得到,而要得到绝对值最大的和,只需让两个前缀和相减得到的差的绝对值最大即可,则直接让最大的前缀和和最小的前缀和相减再取绝对值即得结果。
|
||||
|
||||
### 代码
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int maxAbsoluteSum(vector<int>& nums) {
|
||||
int currentsum = 0;
|
||||
int maxprefix = 0;
|
||||
int minprefix = 0;
|
||||
for(const int& num : nums){
|
||||
currentsum += num;
|
||||
maxprefix = max(maxprefix, currentsum);
|
||||
minprefix = min(minprefix, currentsum);
|
||||
}
|
||||
return maxprefix-minprefix;
|
||||
}
|
||||
};
|
||||
```
|
||||
## day354 2025-02-27
|
||||
### 873. Length of Longest Fibonacci Subsequence
|
||||
A sequence x1, x2, ..., xn is Fibonacci-like if:
|
||||
|
||||
n >= 3
|
||||
xi + xi+1 == xi+2 for all i + 2 <= n
|
||||
Given a strictly increasing array arr of positive integers forming a sequence, return the length of the longest Fibonacci-like subsequence of arr. If one does not exist, return 0.
|
||||
|
||||
A subsequence is derived from another sequence arr by deleting any number of elements (including none) from arr, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].
|
||||
|
||||

|
||||
|
||||
### 题解
|
||||
本题考虑arr数组的长度最大为1000,则可直接暴力求解,因为在遍历时只要知道了前两个数字实际上就等于知道了整个序列,则从头开始遍历数组,选定了首个数字后,再从剩余数组中遍历选定第二个数字,选定第二个数字后即可不断向后计算序列的下一个数字,使用哈希表判断序列的下一个数组在不在数组中即可,如果不在数组中时,将当前已经得到的序列的长度和保存的最大长度比较并更新最大长度。
|
||||
|
||||
### 代码
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int lenLongestFibSubseq(vector<int>& arr) {
|
||||
int n = arr.size();
|
||||
if (n < 3) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
unordered_map<int, int> num_to_index;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
num_to_index[arr[i]] = i;
|
||||
}
|
||||
|
||||
int max_len = 0;
|
||||
for (int i = 0; i < n - 2; ++i) {
|
||||
for (int j = i + 1; j < n - 1; ++j) {
|
||||
int first = arr[i];
|
||||
int second = arr[j];
|
||||
int current_len = 2;
|
||||
|
||||
while (true) {
|
||||
int next_num = first + second;
|
||||
if (num_to_index.count(next_num)) {
|
||||
current_len++;
|
||||
first = second;
|
||||
second = next_num;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
max_len = max(max_len, current_len > 2 ? current_len : 0);
|
||||
}
|
||||
}
|
||||
|
||||
return max_len;
|
||||
}
|
||||
};
|
||||
```
|
||||
## day355 2025-02-28
|
||||
### 1092. Shortest Common Supersequence
|
||||
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.
|
||||
|
||||
A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.
|
||||
|
||||

|
||||
|
||||
### 题解
|
||||
本题的关键在于找出两个字符串的最长公共子字符串,这样只需要在最长公共子字符串的前后补全str1和str2剩余的字符即可得到一个可行解。
|
||||
找出两个字符串的最长公共子字符串可以使用动态规划,构造一个str1.length\*str2.length的二维状态数组,这里使用动态规划是基于一个简单的事实,即如果str1\[i\]和str2\[j\]字符相同,则str1以i结尾和str2以j结尾的字符串中最长的公共子序列长度一定为str1\[i-1]和str2\[j-1\]结尾的子序列长度+1(如果i-1和j-1不同,则i和j处公共子序列长度继承之前的公共子序列长度,如果i-1和j-1结尾的子长度已经为n,那么加上新的相同字符,公共子序列长度一定加1)。记录当前两个字符串中的两个最大子字符串的长度和该最大长度的子串在str1和str2中分别对应的结束位置,随后开始构造目标字符串,将str1和str2中在公共子串前面的字符拼接,再拼接上公共子串,再将两个字符串公共子串后面的字符串拼接,得到最终结果。
|
||||
|
||||
### 代码
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user