mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-19 12:07:34 +08:00
Merge branch 'youngyangyang04:master' into jian
This commit is contained in:
@ -715,26 +715,65 @@ object Solution {
|
||||
### C#:
|
||||
|
||||
```csharp
|
||||
public class Solution {
|
||||
public int[][] GenerateMatrix(int n) {
|
||||
int[][] answer = new int[n][];
|
||||
for(int i = 0; i < n; i++)
|
||||
answer[i] = new int[n];
|
||||
int start = 0;
|
||||
int end = n - 1;
|
||||
int tmp = 1;
|
||||
while(tmp < n * n)
|
||||
public int[][] GenerateMatrix(int n)
|
||||
{
|
||||
// 参考Carl的代码随想录里面C++的思路
|
||||
// https://www.programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html#%E6%80%9D%E8%B7%AF
|
||||
int startX = 0, startY = 0; // 定义每循环一个圈的起始位置
|
||||
int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理
|
||||
int count = 1; // 用来给矩阵每个空格赋值
|
||||
int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2)
|
||||
int offset = 1;// 需要控制每一条边遍历的长度,每次循环右边界收缩一位
|
||||
|
||||
// 构建result二维数组
|
||||
int[][] result = new int[n][];
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for(int i = start; i < end; i++) answer[start][i] = tmp++;
|
||||
for(int i = start; i < end; i++) answer[i][end] = tmp++;
|
||||
for(int i = end; i > start; i--) answer[end][i] = tmp++;
|
||||
for(int i = end; i > start; i--) answer[i][start] = tmp++;
|
||||
start++;
|
||||
end--;
|
||||
result[k] = new int[n];
|
||||
}
|
||||
if(n % 2 == 1) answer[n / 2][n / 2] = tmp;
|
||||
return answer;
|
||||
|
||||
int i = 0, j = 0; // [i,j]
|
||||
while (loop > 0)
|
||||
{
|
||||
i = startX;
|
||||
j = startY;
|
||||
// 四个For循环模拟转一圈
|
||||
// 第一排,从左往右遍历,不取最右侧的值(左闭右开)
|
||||
for (; j < n - offset; j++)
|
||||
{
|
||||
result[i][j] = count++;
|
||||
}
|
||||
// 右侧的第一列,从上往下遍历,不取最下面的值(左闭右开)
|
||||
for (; i < n - offset; i++)
|
||||
{
|
||||
result[i][j] = count++;
|
||||
}
|
||||
|
||||
// 最下面的第一行,从右往左遍历,不取最左侧的值(左闭右开)
|
||||
for (; j > startY; j--)
|
||||
{
|
||||
result[i][j] = count++;
|
||||
}
|
||||
|
||||
// 左侧第一列,从下往上遍历,不取最左侧的值(左闭右开)
|
||||
for (; i > startX; i--)
|
||||
{
|
||||
result[i][j] = count++;
|
||||
}
|
||||
// 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1)
|
||||
startX++;
|
||||
startY++;
|
||||
|
||||
// offset 控制每一圈里每一条边遍历的长度
|
||||
offset++;
|
||||
loop--;
|
||||
}
|
||||
if (n % 2 == 1)
|
||||
{
|
||||
// n 为奇数
|
||||
result[mid][mid] = count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1231,6 +1231,47 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
#### C#:
|
||||
|
||||
```C# 199.二叉树的右视图
|
||||
public class Solution
|
||||
{
|
||||
public IList<int> RightSideView(TreeNode root)
|
||||
{
|
||||
var result = new List<int>();
|
||||
Queue<TreeNode> queue = new();
|
||||
|
||||
if (root != null)
|
||||
{
|
||||
queue.Enqueue(root);
|
||||
}
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
int count = queue.Count;
|
||||
int lastValue = count - 1;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var currentNode = queue.Dequeue();
|
||||
if (i == lastValue)
|
||||
{
|
||||
result.Add(currentNode.val);
|
||||
}
|
||||
|
||||
// lastValue == i == count -1 : left 先于 right 进入Queue
|
||||
if (currentNode.left != null) queue.Enqueue(currentNode.left);
|
||||
if (currentNode.right != null) queue.Enqueue(currentNode.right);
|
||||
|
||||
//// lastValue == i == 0: right 先于 left 进入Queue
|
||||
// if(currentNode.right !=null ) queue.Enqueue(currentNode.right);
|
||||
// if(currentNode.left !=null ) queue.Enqueue(currentNode.left);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 637.二叉树的层平均值
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
|
||||
@ -1558,6 +1599,35 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
#### C#:
|
||||
|
||||
```C# 二叉树的层平均值
|
||||
public class Solution {
|
||||
public IList<double> AverageOfLevels(TreeNode root) {
|
||||
var result= new List<double>();
|
||||
Queue<TreeNode> queue = new();
|
||||
if(root !=null) queue.Enqueue(root);
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
int count = queue.Count;
|
||||
double value=0;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var curentNode=queue.Dequeue();
|
||||
value += curentNode.val;
|
||||
if (curentNode.left!=null) queue.Enqueue(curentNode.left);
|
||||
if (curentNode.right!=null) queue.Enqueue(curentNode.right);
|
||||
}
|
||||
result.Add(value/count);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## 429.N叉树的层序遍历
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
|
||||
|
@ -99,7 +99,7 @@ if (cur == NULL) return cur;
|
||||
|
||||
* 确定单层递归的逻辑
|
||||
|
||||
在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭又闭)
|
||||
在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭右闭)
|
||||
|
||||
那么如果 cur->val 大于 p->val,同时 cur->val 大于q->val,那么就应该向左遍历(说明目标区间在左子树上)。
|
||||
|
||||
|
@ -123,6 +123,9 @@ public:
|
||||
### Java:
|
||||
版本一:使用HashSet
|
||||
```Java
|
||||
// 时间复杂度O(n+m+k) 空间复杂度O(n+k)
|
||||
// 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@ -145,8 +148,15 @@ class Solution {
|
||||
}
|
||||
|
||||
//方法1:将结果集合转为数组
|
||||
|
||||
return resSet.stream().mapToInt(x -> x).toArray();
|
||||
return res.stream().mapToInt(Integer::intValue).toArray();
|
||||
/**
|
||||
* 将 Set<Integer> 转换为 int[] 数组:
|
||||
* 1. stream() : Collection 接口的方法,将集合转换为 Stream<Integer>
|
||||
* 2. mapToInt(Integer::intValue) :
|
||||
* - 中间操作,将 Stream<Integer> 转换为 IntStream
|
||||
* - 使用方法引用 Integer::intValue,将 Integer 对象拆箱为 int 基本类型
|
||||
* 3. toArray() : 终端操作,将 IntStream 转换为 int[] 数组。
|
||||
*/
|
||||
|
||||
//方法2:另外申请一个数组存放setRes中的元素,最后返回数组
|
||||
int[] arr = new int[resSet.size()];
|
||||
@ -538,3 +548,4 @@ end
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -212,13 +212,14 @@ public class Main {
|
||||
int horizontalCut = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
horizontalCut += horizontal[i];
|
||||
result = Math.min(result, Math.abs(sum - 2 * horizontalCut));
|
||||
result = Math.min(result, Math.abs((sum - horizontalCut) - horizontalCut));
|
||||
// 更新result。其中,horizontalCut表示前i行的和,sum - horizontalCut表示剩下的和,作差、取绝对值,得到题目需要的“A和B各自的子区域内的土地总价值之差”。下同。
|
||||
}
|
||||
|
||||
int verticalCut = 0;
|
||||
for (int j = 0; j < m; j++) {
|
||||
verticalCut += vertical[j];
|
||||
result = Math.min(result, Math.abs(sum - 2 * verticalCut));
|
||||
result = Math.min(result, Math.abs((sum - verticalCut) - verticalCut));
|
||||
}
|
||||
|
||||
System.out.println(result);
|
||||
|
Reference in New Issue
Block a user