Skip to content

Commit 5f3a028

Browse files
committed
to: 添加注释
1 parent 4edc8a8 commit 5f3a028

39 files changed

+311
-295
lines changed

src/array/adding_two_negabinary_numbers.cpp

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
11
// 负二进制数相加
2-
// https://leetcode.cn/problems/adding-two-negabinary-numbers
2+
// https://leetcode-cn.com/problems/adding-two-negabinary-numbers
33
// INLINE ../../images/array/adding_two_negabinary_numbers.jpeg
44
// 参见官方题解
55
#include <headers.hpp>
66

7-
// class Solution {
8-
// public:
9-
// vector<int> addNegabinary(vector<int> &arr1, vector<int> &arr2) {
10-
// int i = arr1.size() - 1, j = arr2.size() - 1;
11-
// int carry = 0;
12-
// vector<int> ans;
13-
// while (i >= 0 || j >= 0 || carry) {
14-
// int x = carry;
15-
// if (i >= 0)
16-
// x += arr1[i--];
17-
// if (j >= 0)
18-
// x += arr2[j--];
19-
20-
// ans.push_back((x + 2) % 2);
21-
// carry = -(x / 2);
22-
// }
23-
// while (ans.size() > 1 && ans.back() == 0) {
24-
// ans.pop_back();
25-
// }
26-
// reverse(ans.begin(), ans.end());
27-
// return ans;
28-
// }
29-
// };
30-
317
class Solution {
328
public:
339
vector<int> addNegabinary(vector<int> &arr1, vector<int> &arr2) {

src/array/average_value_of_even_numbers_that_are_divisible_by_three.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
class Solution {
88
public:
99
int averageValue(vector<int> &nums) {
10-
int sum = 0;
11-
int count = 0;
12-
for (auto num : nums) {
10+
int sum = 0; // 初始化数值总和为 0
11+
int count = 0; // 初始化符合条件的数值个数为 0
12+
for (auto num : nums) { // 遍历输入数组中的每一个数值
1313
// 只有两个条件都满足才加入
14-
if (num % 2 == 0 && num % 3 == 0) {
15-
sum += num;
16-
count++;
14+
if (num % 2 == 0 && num % 3 == 0) { // 如果当前数值是偶数且能被 3 整除
15+
sum += num; // 将当前数值加入到数值总和中
16+
count++; // 将符合条件的数值个数加 1
1717
}
1818
}
19-
// 如果没有找到任何满足条件的数,返回 0
19+
// 如果没有找到任何满足条件的数,返回 0,否则返回平均值
2020
return count == 0 ? 0 : sum / count;
2121
}
2222
};

src/array/beautiful_arrangement_ii.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Solution {
99
public:
1010
vector<int> constructArray(int n, int k) {
11-
int interval = k; // interval 为相邻元素的差值
11+
int interval = k; // 相邻元素的差值
1212
vector<int> ans;
1313
ans.push_back(1); // 初始化第一个元素为 1
1414
for (int i = 1; i <= k; i++) {

src/array/best_time_to_buy_and_sell_stock.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 买卖股票的最佳时机
2-
// https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
2+
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
33
// INLINE ../../images/array/best_time_to_buy_and_sell_stock.jpeg
44
#include <headers.hpp>
55

@@ -14,7 +14,9 @@ class Solution {
1414

1515
// 遍历价格数组,更新最大利润和最低价格
1616
for (int price : prices) {
17+
// 更新最大利润,当前价格减去之前的最低价格
1718
maxProfit = max(maxProfit, price - minPrice);
19+
// 更新最低价格,如果当前价格比之前的最低价格还低,则更新为当前价格
1820
minPrice = min(minPrice, price);
1921
}
2022

src/array/check_distances_between_same_letters.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55

66
class Solution {
77
public:
8-
// distance 是一个整数数组,其中的元素表示每个字母之间的最大距离。
9-
// 如果字符串 s 满足以下条件,则返回 true:
10-
// 1. s 的长度为 n,distance 的长度为 m;
11-
// 2. s[i] == s[j] 且 |i - j| < distance[s[i] - 'a'] 对于所有的 0 <= i < j < n
12-
// 都成立。
138
bool checkDistances(string s, vector<int> &distance) {
149
int n = s.size();
1510
int m = distance.size();
1611
// last[i] 表示字符 i 最后出现的位置。
1712
vector<int> last(m, -1);
13+
// 遍历字符串 s,更新 last 数组
1814
for (int i = 0; i < n; ++i) {
1915
int x = s[i] - 'a';
16+
// 如果字符 x 已经出现过,检查距离是否符合要求
2017
if (last[x] != -1 && i - last[x] - 1 != distance[x]) {
2118
return false;
2219
}

src/array/cherry_pickup.cpp

+18-19
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,33 @@ class Solution {
1818
vector<vector<int>> f(
1919
n, vector<int>(n, INT_MIN)); // f 表示俩人摘到的樱桃个数之和的最大值
2020
f[0][0] = grid[0][0];
21-
for (int k = 1; k < n * 2 - 1; ++k) // k为两人步数之和,x为向右走的步数,y为向下走的步数。
22-
// x_1+y_1 = x_2+y_2 = k
23-
{
24-
for (int x1 = min(k, n - 1); x1 >= max(k - n + 1, 0); --x1) {
25-
for (int x2 = min(k, n - 1); x2 >= x1; --x2) {
26-
int y1 = k - x1, y2 = k - x2;
27-
if (grid[x1][y1] == -1 || grid[x2][y2] == -1) {
28-
f[x1][x2] = INT_MIN;
21+
for (int k = 1; k < n * 2 - 1; ++k) { // k为两人步数之和,x为向右走的步数,y为向下走的步数。
22+
// x_1+y_1 = x_2+y_2 = k
23+
for (int x1 = min(k, n - 1); x1 >= max(k - n + 1, 0); --x1) { // 枚举第一个人走的位置
24+
for (int x2 = min(k, n - 1); x2 >= x1; --x2) { // 枚举第二个人走的位置
25+
int y1 = k - x1, y2 = k - x2; // 根据步数和位置,计算出两个人的纵坐标
26+
if (grid[x1][y1] == -1 || grid[x2][y2] == -1) { // 如果当前格子是障碍物,则无法通过
27+
f[x1][x2] = INT_MIN; // 将当前状态设为无穷小,表示无法达到
2928
continue;
3029
}
31-
int res = f[x1][x2]; // 都往右
32-
if (x1) {
33-
res = max(res, f[x1 - 1][x2]); // 往下,往右
30+
int res = f[x1][x2]; // 初始化最大值为当前状态
31+
if (x1) { // 第一个人可以向下走
32+
res = max(res, f[x1 - 1][x2]); // 更新状态,加上向下走的樱桃数
3433
}
35-
if (x2) {
36-
res = max(res, f[x1][x2 - 1]); // 往右,往下
34+
if (x2) { // 第二个人可以向下走
35+
res = max(res, f[x1][x2 - 1]); // 更新状态,加上向下走的樱桃数
3736
}
38-
if (x1 && x2) {
39-
res = max(res, f[x1 - 1][x2 - 1]); // 都往下
37+
if (x1 && x2) { // 两个人都可以向下走
38+
res = max(res, f[x1 - 1][x2 - 1]); // 更新状态,加上向下走的樱桃数
4039
}
41-
res += grid[x1][y1];
40+
res += grid[x1][y1]; // 加上当前格子的樱桃数
4241
if (x2 != x1) { // 避免重复摘同一个樱桃
43-
res += grid[x2][y2];
42+
res += grid[x2][y2]; // 加上第二个人当前格子的樱桃数
4443
}
45-
f[x1][x2] = res;
44+
f[x1][x2] = res; // 更新当前状态
4645
}
4746
}
4847
}
49-
return max(f.back().back(), 0);
48+
return max(f.back().back(), 0); // 返回俩人摘到的樱桃个数之和的最大值,注意要取0和最大值的较大值
5049
}
5150
};

src/array/count_vowel_strings_in_ranges.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ class Solution {
1010
// 计算元音字符串的前缀和。
1111
// 使用前缀和可以避免重复的字符串比较。
1212
int n = words.size();
13-
int prefixSums[n + 1];
14-
memset(prefixSums, 0, sizeof(prefixSums));
15-
for (int i = 0; i < n; i++) {
16-
int value = isVowelString(words[i]) ? 1 : 0;
17-
prefixSums[i + 1] = prefixSums[i] + value;
13+
int prefixSums[n + 1]; // 前缀和数组
14+
memset(prefixSums, 0, sizeof(prefixSums)); // 初始化为0
15+
for (int i = 0; i < n; i++) { // 遍历字符串数组
16+
int value = isVowelString(words[i]) ? 1 : 0; // 判断是否为元音字符串
17+
prefixSums[i + 1] = prefixSums[i] + value; // 计算前缀和
1818
}
1919

2020
// 对于每个查询,返回前缀和的差值。
21-
vector<int> ans(queries.size());
22-
for (int i = 0; i < queries.size(); i++) {
23-
int start = queries[i][0], end = queries[i][1];
24-
ans[i] = prefixSums[end + 1] - prefixSums[start];
21+
vector<int> ans(queries.size()); // 用于存储查询结果的数组
22+
for (int i = 0; i < queries.size(); i++) { // 遍历查询数组
23+
int start = queries[i][0], end = queries[i][1]; // 获取查询范围
24+
ans[i] = prefixSums[end + 1] - prefixSums[start]; // 计算查询结果
2525
}
26-
return ans;
26+
return ans; // 返回查询结果数组
2727
}
2828

2929
// 检查字符串是否为元音字符串。
3030
bool isVowelString(const string &word) {
31-
return isVowelLetter(word[0]) && isVowelLetter(word.back());
31+
return isVowelLetter(word[0]) && isVowelLetter(word.back()); // 判断首尾字母是否为元音字母
3232
}
3333

3434
// 检查字母是否为元音字母。
3535
bool isVowelLetter(char c) {
36-
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
36+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; // 判断是否为元音字母
3737
}
3838
};

src/array/determine_if_two_events_have_conflict.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
class Solution {
77
public:
88
bool haveConflict(vector<string> &event1, vector<string> &event2) {
9-
// 事件1开始时间大于等于事件2的结束时间,或者事件1的结束时间小于等于事件2的开始时间,说明没有冲突
9+
// 如果事件1的开始时间大于等于事件2的结束时间,或者事件1的结束时间小于等于事件2的开始时间,说明没有冲突
10+
// 否则就存在冲突
1011
return !(event1[1] < event2[0] || event2[1] < event1[0]);
1112
}
1213
};

src/array/filling_bookcase_shelves.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ class Solution {
1515
int height = books[i - 1][1];
1616
// 首先考虑将第i本书单独放置在一层
1717
dp[i] = dp[i - 1] + height;
18+
// 从i-1本书开始往前遍历,尝试将第j到第i本书一起放置在一层
1819
for (int j = i - 1; j > 0 && width + books[j - 1][0] <= shelfWidth; j--) {
19-
// 将第j到第i本书一起放置在一层
20+
// 更新该层的高度为最高的一本书的高度
2021
height = max(height, books[j - 1][1]);
22+
// 更新该层的宽度为放置的所有书的总宽度
2123
width += books[j - 1][0];
24+
// 更新dp[i]为更小的值
2225
dp[i] = min(dp[i], dp[j - 1] + height);
2326
}
2427
}

src/array/find_the_kth_smallest_sum_of_a_matrix_with_sorted_rows.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 有序矩阵中的第 k 个最小数组和
2-
// https://leetcode.cn/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows
2+
// https://leetcode-cn.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows
33
// INLINE
44
// ../../images/array/find_the_kth_smallest_sum_of_a_matrix_with_sorted_rows.jpeg
55
#include <headers.hpp>
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
// 字符串中的第一个唯一字符
2-
// https://leetcode.cn/problems/first-unique-character-in-a-string/
2+
// https://leetcode-cn.com/problems/first-unique-character-in-a-string/
33
// INLINE ../../images/array/first_unique_character_in_a_string.jpeg
44
#include <headers.hpp>
55

66
class Solution {
77
public:
88
int firstUniqChar(string s) {
9+
// 统计每个字符出现的频率
910
unordered_map<int, int> frequency;
1011
for (auto ch : s) {
1112
frequency[ch]++;
1213
}
1314

15+
// 遍历字符串,找到第一个出现频率为1的字符
1416
for (int i = 0; i < s.length(); i++) {
1517
if (frequency[s[i]] == 1) {
16-
// return s[i];
18+
// 返回该字符的索引
1719
return i;
1820
}
1921
}
2022

23+
// 如果没有找到出现频率为1的字符,返回-1
2124
return -1;
2225
}
2326
};

src/array/flip_columns_for_maximum_number_of_equal_rows.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
class Solution {
77
public:
88
int maxEqualRowsAfterFlips(vector<vector<int>> &matrix) {
9-
int m = matrix.size(), n = matrix[0].size();
10-
vector<int> flip(n);
11-
int ans = 0;
12-
for (int i = 0; i < m; i++) {
13-
int cnt = 0;
14-
for (int j = 0; j < n; j++) {
15-
flip[j] = 1 - matrix[i][j];
9+
int m = matrix.size(), n = matrix[0].size(); // 获取行列数
10+
vector<int> flip(n); // 定义一个大小为n的vector,用来存储翻转后的列
11+
int ans = 0; // 初始化答案
12+
for (int i = 0; i < m; i++) { // 遍历每一行
13+
int cnt = 0; // 初始化计数器
14+
for (int j = 0; j < n; j++) { // 遍历每一列
15+
flip[j] = 1 - matrix[i][j]; // 计算翻转后的列
1616
}
17-
for (int k = 0; k < m; k++) {
18-
if (matrix[k] == matrix[i] || matrix[k] == flip) {
19-
cnt++;
17+
for (int k = 0; k < m; k++) { // 再次遍历每一行
18+
if (matrix[k] == matrix[i] || matrix[k] == flip) { // 如果该行和当前行相同,或者和当前行翻转后相同
19+
cnt++; // 计数器加1
2020
}
2121
}
22-
ans = max(ans, cnt);
22+
ans = max(ans, cnt); // 更新答案
2323
}
24-
return ans;
24+
return ans; // 返回最终答案
2525
}
2626
};

src/array/length_of_longest_fibonacci_subsequence.cpp

+21-24
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,37 @@
22
// https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence
33
// INLINE ../../images/array/length_of_longest_fibonacci_subsequence.jpeg
44
// 解题思路:见代码,常规暴力法优化查找时间。
5+
56
#include <headers.hpp>
67

7-
class Solution
8-
{
8+
class Solution {
99
public:
10-
int lenLongestFibSubseq(vector<int> &arr)
11-
{
10+
int lenLongestFibSubseq(vector<int> &arr) {
1211
int maxLen = 0;
1312
// 以空间换时间。因为序列已经排好序,因此把所有数值放到Map降低第三个数查找的时间复杂度。
14-
unordered_set<int> set; // 如果用二分查找则第三个数是O(log n),用Set则为O(1)。
13+
unordered_set<int>
14+
set; // 如果用二分查找则第三个数是O(log n),用Set则为O(1)。
1515
int len = arr.size();
16-
for (int i = 0; i < len; i++)
17-
{
16+
for (int i = 0; i < len; i++) {
1817
set.emplace(arr[i]);
1918
}
20-
for (int i = 0; i < len - 2; i++)
21-
{
22-
for (int j = i + 1; j < len - 1; j++)
23-
{
24-
int first = arr[i];
25-
int second = arr[j];
26-
int third = first + second;
27-
int currentLen = 2; // 按题目规定,长度至少为3,如果前两个数已经确定,则长度至少为2。
28-
while (set.find(third) != set.end())
29-
{
30-
currentLen++;
31-
first = second;
32-
second = third;
33-
third = first + second;
19+
for (int i = 0; i < len - 2; i++) {
20+
for (int j = i + 1; j < len - 1; j++) {
21+
int first = arr[i]; // 第一个数
22+
int second = arr[j]; // 第二个数
23+
int third = first + second; // 第三个数
24+
int currentLen =
25+
2; // 按题目规定,长度至少为3,如果前两个数已经确定,则长度至少为2。
26+
while (set.find(third) != set.end()) { // 第三个数在序列中
27+
currentLen++; // 长度加1
28+
first = second; // 第一个数变为第二个数
29+
second = third; // 第二个数变为第三个数
30+
third = first + second; // 第三个数变为新的第一个数加第二个数的和
3431
}
35-
maxLen = max(currentLen, maxLen);
32+
maxLen = max(currentLen, maxLen); // 更新最长长度
3633
}
3734
}
38-
39-
return maxLen >= 3 ? maxLen : 0; // 按题目规定,长度至少为3,如果一个不存在,返回0。
35+
return maxLen >= 3 ? maxLen
36+
: 0; // 按题目规定,长度至少为3,如果一个不存在,返回0。
4037
}
4138
};

0 commit comments

Comments
 (0)