leetcode
这周太多ddl,而且临近期末,各科"预习"压力较大,学习和做题的进度有些慢.(也没看到要写bfs,dfs)
大致了解了哈希(弱鸡的我现在才学哈希),贪心还有温习了一下dp(就算几道股票题:含手续费,最佳时机)
关于dfs,感觉所接触到的题用bfs,有一部分套路在内
N叉树最大深度
思路:
二叉树的变式,与此前的二叉树最大深度差不多
BFS:
/**
* Definition for a Node.
* struct Node {
* int val;
* int numChildren;
* struct Node** children;
* };
*/
struct Node* que[100001];
int maxDepth(struct Node* root) {
if(!root) return 0;
int right=0,left=0;
int depth=0;
struct Node *tmp=NULL;
int len;
que[right++]=root;
while(left<right){
len = right-left;
depth++;
for(int i=0;i<len;i++){
tmp=que[left++];
for(int j=0;j<tmp->numChildren;j++){
if(tmp->children[j]) que[right++] = tmp->children[j];
}
}
}
return depth;
}
DFS:
#define MAX(a,b) (a>b)?a:b;
int maxDepth(struct Node* root) {
if(!root) return 0;
int height = 0;
int temp;
for(int i=0;i<root->numChildren;i++){
temp = maxDepth(root->children[i]);
height = MAX(height,temp);
}
return height+1;
}
填充右侧指针
思路:
层序遍历,利用各节点入队的顺序,右侧节点会位于同层左侧节点后一位,所以只需让队列中左侧节点指向下一位即可,若右侧无节点,则队列中下一位为空
struct Node *que[100001];
struct Node* connect(struct Node* root) {
if(!root) return 0;
int left=0,right=0;
que[right++] = root;
while(left<right){
int len = right-left;
for(int i=0;i<len;i++){
struct Node *node = que[left++];
if(i<(len-1)) node->next = que[left];
if(node->left) que[right++] = node->left;
if(node->right) que[right++] = node->right;
}
}
return root;
}
比赛
周四的程序设计比赛,第一次和同学组队,8/15题.
做了6到题,但又两道tle一道wa.
存在的问题:
1.比赛中对于C++各种函数的运用存在一些问题,sort之类的基础函数没有熟练,且对于存在字符串的题目可以说是望而却步,全都交由队友完成,我主要完成含数学运算类型的题目.对于字符串方面应加强训练.
2.比赛中有道图论的题目,计算各节点最短路径,有点没信心完成吧,没有选择,但明明是已经接触过的知识.反而是另一名还没学过的队友接下了这道题并ac.怎么说呢,应该是知识点没熟练吧,当时也紧张,面对那道题头脑有些空白.向大佬队友学习吧,再提高一些熟练度,发展自己短期内获取信息的能力.
3.本次比赛出现最多的问题就是数据类型的问题.我ac的那几道题,都出现过一个错误,就是没有考虑较大数据的情况,定义时没有用long,或者该用long的用了int,该用int的用了long.导致队伍被罚时,也导致有道题wa且没看出是long的问题,拖慢了做题速度.
比赛的时候,不够冷静不够细心是一部分,平时知识点的积累和巩固也是一部分.