Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5973a5

Browse files
Merge branch 'youngyangyang04:master' into jian
2 parents 467e6c7 + 9f203fb commit a5973a5

File tree

5 files changed

+144
-23
lines changed

5 files changed

+144
-23
lines changed

‎problems/0059.螺旋矩阵II.md

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -715,26 +715,65 @@ object Solution {
715715
### C#:
716716

717717
```csharp
718-
public class Solution {
719-
public int[][] GenerateMatrix(int n) {
720-
int[][] answer = new int[n][];
721-
for(int i = 0; i < n; i++)
722-
answer[i] = new int[n];
723-
int start = 0;
724-
int end = n - 1;
725-
int tmp = 1;
726-
while(tmp < n * n)
718+
public int[][] GenerateMatrix(int n)
719+
{
720+
// 参考Carl的代码随想录里面C++的思路
721+
// https://www.programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html#%E6%80%9D%E8%B7%AF
722+
int startX = 0, startY = 0; // 定义每循环一个圈的起始位置
723+
int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理
724+
int count = 1; // 用来给矩阵每个空格赋值
725+
int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2)
726+
int offset = 1;// 需要控制每一条边遍历的长度,每次循环右边界收缩一位
727+
728+
// 构建result二维数组
729+
int[][] result = new int[n][];
730+
for (int k = 0; k < n; k++)
731+
{
732+
result[k] = new int[n];
733+
}
734+
735+
int i = 0, j = 0; // [i,j]
736+
while (loop > 0)
737+
{
738+
i = startX;
739+
j = startY;
740+
// 四个For循环模拟转一圈
741+
// 第一排,从左往右遍历,不取最右侧的值(左闭右开)
742+
for (; j < n - offset; j++)
743+
{
744+
result[i][j] = count++;
745+
}
746+
// 右侧的第一列,从上往下遍历,不取最下面的值(左闭右开)
747+
for (; i < n - offset; i++)
748+
{
749+
result[i][j] = count++;
750+
}
751+
752+
// 最下面的第一行,从右往左遍历,不取最左侧的值(左闭右开)
753+
for (; j > startY; j--)
754+
{
755+
result[i][j] = count++;
756+
}
757+
758+
// 左侧第一列,从下往上遍历,不取最左侧的值(左闭右开)
759+
for (; i > startX; i--)
727760
{
728-
for(int i = start; i < end; i++) answer[start][i] = tmp++;
729-
for(int i = start; i < end; i++) answer[i][end] = tmp++;
730-
for(int i = end; i > start; i--) answer[end][i] = tmp++;
731-
for(int i = end; i > start; i--) answer[i][start] = tmp++;
732-
start++;
733-
end--;
734-
}
735-
if(n % 2 == 1) answer[n / 2][n / 2] = tmp;
736-
return answer;
761+
result[i][j] = count++;
762+
}
763+
// 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1)
764+
startX++;
765+
startY++;
766+
767+
// offset 控制每一圈里每一条边遍历的长度
768+
offset++;
769+
loop--;
770+
}
771+
if (n % 2 == 1)
772+
{
773+
// n 为奇数
774+
result[mid][mid] = count;
737775
}
776+
return result;
738777
}
739778
```
740779

‎problems/0102.二叉树的层序遍历.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,47 @@ impl Solution {
12311231
}
12321232
```
12331233

1234+
#### C#:
1235+
1236+
```C# 199.二叉树的右视图
1237+
public class Solution
1238+
{
1239+
public IList<int> RightSideView(TreeNode root)
1240+
{
1241+
var result = new List<int>();
1242+
Queue<TreeNode> queue = new();
1243+
1244+
if (root != null)
1245+
{
1246+
queue.Enqueue(root);
1247+
}
1248+
while (queue.Count > 0)
1249+
{
1250+
int count = queue.Count;
1251+
int lastValue = count - 1;
1252+
for (int i = 0; i < count; i++)
1253+
{
1254+
var currentNode = queue.Dequeue();
1255+
if (i == lastValue)
1256+
{
1257+
result.Add(currentNode.val);
1258+
}
1259+
1260+
// lastValue == i == count -1 : left 先于 right 进入Queue
1261+
if (currentNode.left != null) queue.Enqueue(currentNode.left);
1262+
if (currentNode.right != null) queue.Enqueue(currentNode.right);
1263+
1264+
//// lastValue == i == 0: right 先于 left 进入Queue
1265+
// if(currentNode.right !=null ) queue.Enqueue(currentNode.right);
1266+
// if(currentNode.left !=null ) queue.Enqueue(currentNode.left);
1267+
}
1268+
}
1269+
1270+
return result;
1271+
}
1272+
}
1273+
```
1274+
12341275
## 637.二叉树的层平均值
12351276

12361277
[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
@@ -1558,6 +1599,35 @@ impl Solution {
15581599
}
15591600
```
15601601

1602+
#### C#:
1603+
1604+
```C# 二叉树的层平均值
1605+
public class Solution {
1606+
public IList<double> AverageOfLevels(TreeNode root) {
1607+
var result= new List<double>();
1608+
Queue<TreeNode> queue = new();
1609+
if(root !=null) queue.Enqueue(root);
1610+
1611+
while (queue.Count > 0)
1612+
{
1613+
int count = queue.Count;
1614+
double value=0;
1615+
for (int i = 0; i < count; i++)
1616+
{
1617+
var curentNode=queue.Dequeue();
1618+
value += curentNode.val;
1619+
if (curentNode.left!=null) queue.Enqueue(curentNode.left);
1620+
if (curentNode.right!=null) queue.Enqueue(curentNode.right);
1621+
}
1622+
result.Add(value/count);
1623+
}
1624+
1625+
return result;
1626+
}
1627+
}
1628+
1629+
```
1630+
15611631
## 429.N叉树的层序遍历
15621632

15631633
[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)

‎problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ if (cur == NULL) return cur;
9999

100100
* 确定单层递归的逻辑
101101

102-
在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭又闭)
102+
在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭右闭)
103103

104104
那么如果 cur->val 大于 p->val,同时 cur->val 大于q->val,那么就应该向左遍历(说明目标区间在左子树上)。
105105

‎problems/0349.两个数组的交集.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public:
123123
### Java:
124124
版本一:使用HashSet
125125
```Java
126+
// 时间复杂度O(n+m+k) 空间复杂度O(n+k)
127+
// 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数
128+
126129
import java.util.HashSet;
127130
import java.util.Set;
128131

@@ -145,8 +148,15 @@ class Solution {
145148
}
146149

147150
//方法1:将结果集合转为数组
148-
149-
return resSet.stream().mapToInt(x -> x).toArray();
151+
return res.stream().mapToInt(Integer::intValue).toArray();
152+
/**
153+
* 将 Set<Integer> 转换为 int[] 数组:
154+
* 1. stream() : Collection 接口的方法,将集合转换为 Stream<Integer>
155+
* 2. mapToInt(Integer::intValue) :
156+
* - 中间操作,将 Stream<Integer> 转换为 IntStream
157+
* - 使用方法引用 Integer::intValue,将 Integer 对象拆箱为 int 基本类型
158+
* 3. toArray() : 终端操作,将 IntStream 转换为 int[] 数组。
159+
*/
150160

151161
//方法2:另外申请一个数组存放setRes中的元素,最后返回数组
152162
int[] arr = new int[resSet.size()];
@@ -538,3 +548,4 @@ end
538548
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
539549
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
540550
</a>
551+

‎problems/kamacoder/0044.开发商购买土地.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ public class Main {
212212
int horizontalCut = 0;
213213
for (int i = 0; i < n; i++) {
214214
horizontalCut += horizontal[i];
215-
result = Math.min(result, Math.abs(sum - 2 * horizontalCut));
215+
result = Math.min(result, Math.abs((sum - horizontalCut) - horizontalCut));
216+
// 更新result。其中,horizontalCut表示前i行的和,sum - horizontalCut表示剩下的和,作差、取绝对值,得到题目需要的"A和B各自的子区域内的土地总价值之差"。下同。
216217
}
217218

218219
int verticalCut = 0;
219220
for (int j = 0; j < m; j++) {
220221
verticalCut += vertical[j];
221-
result = Math.min(result, Math.abs(sum - 2* verticalCut));
222+
result = Math.min(result, Math.abs((sum - verticalCut) - verticalCut));
222223
}
223224

224225
System.out.println(result);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /