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 588e42c

Browse files
Merge branch 'jian' of github.com:JianWangUCD/leetcode-master into jian
2 parents a66142c + a5973a5 commit 588e42c

13 files changed

+390
-66
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/0070.爬楼梯.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public:
130130
};
131131
```
132132
133-
* 时间复杂度:$O(n)$
134-
* 空间复杂度:$O(n)$
133+
* 时间复杂度:O(n)
134+
* 空间复杂度:O(n)
135135
136136
当然依然也可以,优化一下空间复杂度,代码如下:
137137
@@ -154,8 +154,8 @@ public:
154154
};
155155
```
156156

157-
* 时间复杂度:$O(n)$
158-
* 空间复杂度:$O(1)$
157+
* 时间复杂度:O(n)
158+
* 空间复杂度:O(1)
159159

160160
后面将讲解的很多动规的题目其实都是当前状态依赖前两个,或者前三个状态,都可以做空间上的优化,**但我个人认为面试中能写出版本一就够了哈,清晰明了,如果面试官要求进一步优化空间的话,我们再去优化**
161161

@@ -524,3 +524,4 @@ impl Solution {
524524
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
525525
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
526526
</a>
527+

‎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/0111.二叉树的最小深度.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。
4141

4242
* 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
43-
* 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)
43+
* 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
4444

4545
那么使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,不过这个最小距离 也同样是最小深度。
4646

‎problems/0150.逆波兰表达式求值.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,67 @@ impl Solution {
489489
}
490490
```
491491

492+
### C:
493+
494+
```c
495+
int str_to_int(char *str) {
496+
// string转integer
497+
int num = 0, tens = 1;
498+
for (int i = strlen(str) - 1; i >= 0; i--) {
499+
if (str[i] == '-') {
500+
num *= -1;
501+
break;
502+
}
503+
num += (str[i] - '0') * tens;
504+
tens *= 10;
505+
}
506+
return num;
507+
}
508+
509+
int evalRPN(char** tokens, int tokensSize) {
510+
511+
int *stack = (int *)malloc(tokensSize * sizeof(int));
512+
assert(stack);
513+
int stackTop = 0;
514+
515+
for (int i = 0; i < tokensSize; i++) {
516+
char symbol = (tokens[i])[0];
517+
if (symbol < '0' && (tokens[i])[1] == '0円') {
518+
519+
// pop两个数字
520+
int num1 = stack[--stackTop];
521+
int num2 = stack[--stackTop];
522+
523+
// 计算结果
524+
int result;
525+
if (symbol == '+') {
526+
result = num1 + num2;
527+
} else if (symbol == '-') {
528+
result = num2 - num1;
529+
} else if (symbol == '/') {
530+
result = num2 / num1;
531+
} else {
532+
result = num1 * num2;
533+
}
534+
535+
// push回stack
536+
stack[stackTop++] = result;
537+
538+
} else {
539+
540+
// push数字进stack
541+
int num = str_to_int(tokens[i]);
542+
stack[stackTop++] = num;
543+
544+
}
545+
}
546+
547+
int result = stack[0];
548+
free(stack);
549+
return result;
550+
}
551+
```
552+
492553
<p align="center">
493554
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
494555
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

‎problems/0225.用队列实现栈.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,95 @@ impl MyStack {
12771277
}
12781278
```
12791279

1280+
### C:
1281+
1282+
> C:单队列
1283+
1284+
```c
1285+
typedef struct Node {
1286+
int val;
1287+
struct Node *next;
1288+
} Node_t;
1289+
1290+
// 用单向链表实现queue
1291+
typedef struct {
1292+
Node_t *head;
1293+
Node_t *foot;
1294+
int size;
1295+
} MyStack;
1296+
1297+
MyStack* myStackCreate() {
1298+
MyStack *obj = (MyStack *)malloc(sizeof(MyStack));
1299+
assert(obj);
1300+
obj->head = NULL;
1301+
obj->foot = NULL;
1302+
obj->size = 0;
1303+
return obj;
1304+
}
1305+
1306+
void myStackPush(MyStack* obj, int x) {
1307+
1308+
Node_t *temp = (Node_t *)malloc(sizeof(Node_t));
1309+
assert(temp);
1310+
temp->val = x;
1311+
temp->next = NULL;
1312+
1313+
// 添加至queue末尾
1314+
if (obj->foot) {
1315+
obj->foot->next = temp;
1316+
} else {
1317+
obj->head = temp;
1318+
}
1319+
obj->foot = temp;
1320+
obj->size++;
1321+
}
1322+
1323+
int myStackPop(MyStack* obj) {
1324+
1325+
// 获取末尾元素
1326+
int target = obj->foot->val;
1327+
1328+
if (obj->head == obj->foot) {
1329+
free(obj->foot);
1330+
obj->head = NULL;
1331+
obj->foot = NULL;
1332+
} else {
1333+
1334+
Node_t *prev = obj->head;
1335+
// 移动至queue尾部节点前一个节点
1336+
while (prev->next != obj->foot) {
1337+
prev = prev->next;
1338+
}
1339+
1340+
free(obj->foot);
1341+
obj->foot = prev;
1342+
obj->foot->next = NULL;
1343+
}
1344+
1345+
obj->size--;
1346+
return target;
1347+
}
1348+
1349+
int myStackTop(MyStack* obj) {
1350+
return obj->foot->val;
1351+
}
1352+
1353+
bool myStackEmpty(MyStack* obj) {
1354+
return obj->size == 0;
1355+
}
1356+
1357+
void myStackFree(MyStack* obj) {
1358+
Node_t *curr = obj->head;
1359+
while (curr != NULL) {
1360+
Node_t *temp = curr->next;
1361+
free(curr);
1362+
curr = temp;
1363+
}
1364+
free(obj);
1365+
}
1366+
1367+
```
1368+
12801369
12811370
<p align="center">
12821371
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎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

0 commit comments

Comments
(0)

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