Skip to content

Commit 2ff0d1f

Browse files
committed
to: 添加注释
1 parent 5f3a028 commit 2ff0d1f

11 files changed

+68
-35
lines changed

src/array/adding_two_negabinary_numbers.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
// 负二进制数相加
2-
// https://leetcode-cn.com/problems/adding-two-negabinary-numbers
2+
// https://leetcode.cn/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+
731
class Solution {
832
public:
933
vector<int> addNegabinary(vector<int> &arr1, vector<int> &arr2) {

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; // 相邻元素的差值
11+
int interval = k; // interval 为相邻元素的差值
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 买卖股票的最佳时机
2-
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
2+
// https://leetcode.cn/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

src/array/check_distances_between_same_letters.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
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+
// 都成立。
813
bool checkDistances(string s, vector<int> &distance) {
914
int n = s.size();
1015
int m = distance.size();

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.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows
2+
// https://leetcode.cn/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>

src/array/first_unique_character_in_a_string.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 字符串中的第一个唯一字符
2-
// https://leetcode-cn.com/problems/first-unique-character-in-a-string/
2+
// https://leetcode.cn/problems/first-unique-character-in-a-string/
33
// INLINE ../../images/array/first_unique_character_in_a_string.jpeg
44
#include <headers.hpp>
55

src/array/longest_arithmetic_subsequence.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// 最长等差数列
2-
// https://leetcode-cn/problems/longest-arithmetic-subsequence
2+
// https://leetcode.cn/problems/longest-arithmetic-subsequence
33
// INLINE ../../images/array/longest_arithmetic_subsequence.jpeg
44
#include <headers.hpp>
55

66
class Solution {
77
public:
88
int longestArithSeqLength(vector<int> &nums) {
9-
int ans = 2; // 最小长度为2
9+
int ans = 2; // 最小长度为2
1010
int n = nums.size(); // 数组大小
11-
vector<unordered_map<int, int>> dp(n); // 定义dp,dp[i][d]表示以i为结尾,差值为d的最长等差数列长度
12-
for (int i = 0; i < n; i++) { // 枚举结尾
11+
vector<unordered_map<int, int>> dp(
12+
n); // 定义dp,dp[i][d]表示以i为结尾,差值为d的最长等差数列长度
13+
for (int i = 0; i < n; i++) { // 枚举结尾
1314
for (int j = i + 1; j < n; j++) { // 枚举倒数第二个数
14-
int d = nums[j] - nums[i]; // 求差值
15+
int d = nums[j] - nums[i]; // 求差值
1516
if (dp[i].count(d)) { // 如果存在以i为结尾,差值为d的等差数列
16-
dp[j][d] = dp[i][d] + 1; // 那么以j为结尾,差值为d的等差数列长度为dp[i][d]+1
17+
dp[j][d] =
18+
dp[i][d] + 1; // 那么以j为结尾,差值为d的等差数列长度为dp[i][d]+1
1719
} else {
1820
dp[j][d] = 2; // 否则,以j为结尾,差值为d的等差数列长度为2
1921
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// 删除某些元素后的数组均值
2-
// https://leetcode-cn.com/problems/mean-of-array-after-removing-some-elements/
2+
// https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/
33
// INLINE ../../images/array/mean_of_array_after_removing_some_elements.jpeg
44
#include <headers.hpp>
55
#include <numeric>
66

77
class Solution {
88
public:
99
double trimMean(vector<int> &arr) {
10-
int len = arr.size(); // 数组长度
10+
int len = arr.size(); // 数组长度
1111
sort(arr.begin(), arr.end()); // 对数组进行排序
12-
double res =
13-
accumulate(arr.begin() + len / 20, arr.begin() + 0.95 * len, 0); // 去掉前后各5%的元素后,求剩余元素的和
12+
double res = accumulate(arr.begin() + len / 20, arr.begin() + 0.95 * len,
13+
0); // 去掉前后各5%的元素后,求剩余元素的和
1414
return res / (len * 0.9); // 求剩余元素的平均值
1515
}
1616
};

src/array/pascals_triangle.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
// 杨辉三角
2-
// https://leetcode-cn.com/problems/pascals-triangle
2+
// https://leetcode.cn/problems/pascals-triangle
33
// INLINE ../../images/array/pascals_triangle.jpeg
44

55
#include <headers.hpp>
66

77
class Solution {
88
public:
99
vector<vector<int>> generate(int numRows) {
10-
vector<vector<int>> ret(numRows); // 创建一个二维vector,其中第一维长度为numRows,第二维长度为i+1
10+
vector<vector<int>> ret(
11+
numRows); // 创建一个二维vector,其中第一维长度为numRows,第二维长度为i+1
1112
for (int i = 0; i < numRows; ++i) { // 循环numRows次,每次处理一行
12-
ret[i].resize(i + 1); // 第i行的长度为i+1
13+
ret[i].resize(i + 1); // 第i行的长度为i+1
1314
ret[i][0] = ret[i][i] = 1; // 第i行的第一个和最后一个元素都是1
1415
for (int j = 1; j < i; ++j) { // 处理第i行的第1~i-1个元素
15-
ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1]; // 第i行第j个元素等于第i-1行第j个元素和第i-1行第j-1个元素之和
16+
ret[i][j] =
17+
ret[i - 1][j] +
18+
ret[i - 1]
19+
[j -
20+
1]; // 第i行第j个元素等于第i-1行第j个元素和第i-1行第j-1个元素之和
1621
}
1722
}
1823
return ret; // 返回二维vector

src/array/rank_transform_of_an_array.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
// 数组序号转换
2-
// https://leetcode-cn/problems/rank-transform-of-an-array
2+
// https://leetcode.cn/problems/rank-transform-of-an-array
33
// INLINE ../../images/array/rank_transform_of_an_array.jpeg
44
#include <headers.hpp>
55

6-
class Solution
7-
{
6+
class Solution {
87
public:
9-
vector<int> arrayRankTransform(vector<int> &arr)
10-
{
11-
vector<int> sArr = arr; // 复制原数组
8+
vector<int> arrayRankTransform(vector<int> &arr) {
9+
vector<int> sArr = arr; // 复制原数组
1210
sort(sArr.begin(), sArr.end()); // 对复制数组进行排序
13-
unordered_map<int, int> ranks; // 记录每个元素的排名
14-
vector<int> ans(arr.size()); // 存储排名后的结果
15-
for (auto &a : sArr)
16-
{
11+
unordered_map<int, int> ranks; // 记录每个元素的排名
12+
vector<int> ans(arr.size()); // 存储排名后的结果
13+
for (auto &a : sArr) {
1714
if (!ranks.count(a)) // 如果该元素还未出现过
1815
{
1916
ranks[a] = ranks.size() + 1; // 给它一个排名
2017
}
2118
}
22-
for (int i = 0; i < arr.size(); i++)
23-
{
19+
for (int i = 0; i < arr.size(); i++) {
2420
ans[i] = ranks[arr[i]]; // 获取原数组每个元素的排名
2521
}
2622
return ans; // 返回排名后的结果

src/array/special_array_with_x_elements_greater_than_or_equal_x.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 特殊数组的特征值
2-
// https://leetcode-cn/problems/special-array-with-x-elements-greater-than-or-equal-x
2+
// https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x
33
// INLINE
44
// ../../images/array/special_array_with_x_elements_greater_than_or_equal_x.jpeg
55
// 解题思路:本地画图模拟
@@ -11,9 +11,10 @@ class Solution {
1111
sort(nums.begin(), nums.end()); // 先将数组排序
1212
int n = nums.size();
1313
for (int i = 0; i < n; i++) { // 遍历数组
14-
int res = n - i; // 计算特征值
15-
if (nums[i] >= res && (i - 1 < 0 || nums[i - 1] < res)) { // 判断是否符合条件
16-
return res; // 返回特征值
14+
int res = n - i; // 计算特征值
15+
if (nums[i] >= res &&
16+
(i - 1 < 0 || nums[i - 1] < res)) { // 判断是否符合条件
17+
return res; // 返回特征值
1718
}
1819
}
1920
return -1; // 找不到符合条件的特征值,返回-1

0 commit comments

Comments
 (0)