102 篇文章 8 订阅

# 2022-01-26每日刷题打卡

## 飞书——每日一题

#### 494. 目标和

-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3

class Solution {
public:
int count=0;
int findTargetSumWays(vector<int>& nums, int target) {
int sum=0,ans=0;
bfs(nums,target,sum,ans);
return count;
}
void bfs(vector<int>& nums, int target,int sum,int ans)
{
if(ans==nums.size())
{
if(sum==target)count++;
return;
}
bfs(nums,target,sum+nums[ans],ans+1);
bfs(nums,target,sum-nums[ans],ans+1);
}
};


#### 429. N 叉树的层序遍历

/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;

Node() {}

Node(int _val) {
val = _val;
}

Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/

class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>>v;
if(!root)return v;
queue<Node*>que;
que.push(root);
while(que.size())
{
int len=que.size();
vector<int>ans;
for(int i=0;i<len;i++)
{
ans.push_back(que.front()->val);
if(que.front()->children.size())
{
for(int i=0;i<que.front()->children.size();i++)
{
que.push(que.front()->children[i]);
}
}
que.pop();
}

v.push_back(ans);
}
return v;
}
};


#### 797. 所有可能的路径

graph[i] 是一个从节点 i 可以访问的所有节点的列表（即从节点 i 到节点 graph[ i] [ j]存在一条有向边）。

class Solution {
public:
vector<vector<int>>v;
vector<int>ans;
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
ans.push_back(0);
bfs(graph,0,graph.size()-1);
return v;
}
void bfs(vector<vector<int>>& graph,int x,int n)
{
if(x==n)
{
v.push_back(ans);
return;
}
for(auto i:graph[x])
{
ans.push_back(i);
bfs(graph,i,n);
ans.pop_back();
}
}
};


#### 802. 找到最终的安全状态

class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
int n=graph.size();
vector<vector<int>>v(n);
vector<int>rd(n);
for(int i=0;i<n;i++)
{
for(auto j:graph[i])
{
v[j].push_back(i);
}
rd[i]=graph[i].size();
}
queue<int>que;
for(int i=0;i<n;i++)
{
if(rd[i]==0)
que.push(i);
}
while(que.size())
{
int t=que.front();
que.pop();
for(int i=0;i<v[t].size();i++)
{
rd[v[t][i]]--;
if(rd[v[t][i]]==0)
{
que.push(v[t][i]);
}
}
}
vector<int>ans;
for(int i=0;i<n;i++)
{
if(rd[i]==0)ans.push_back(i);
}
return ans;
}
};


#### 841. 钥匙和房间

class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
int n=rooms.size();
vector<int>v(n);
v[0]=1;
bfs(rooms,v,0);
for(int i=0;i<n;i++)
{
if(v[i]==0)return false;
}
return true;
}
void bfs(vector<vector<int>>& rooms,vector<int>& v,int u)
{
for(auto i:rooms[u])
{
if(v[i]!=0)continue;
v[i]=1;
bfs(rooms,v,i);
}
}
};


#### 865. 具有所有最深节点的最小子树

nmd这个题目描述，搞不懂什么是最小子树，后来看了评论才知道，就是说如果这个树的左右子节点都是最大深度的话，这个节点就是最小子树（人话就是最大深度节点的公共祖先）。比如这里，最大深度的节点是7和4，这两个节点的公告祖先就是2。如果是

  0
/ \
1   2
\
3


/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
if(!root)return root;
int left=getDeep(root->left);
int right=getDeep(root->right);
if(left==right)return root;
else if(left>right)return subtreeWithAllDeepest(root->left);
else return subtreeWithAllDeepest(root->right);
}
int getDeep(TreeNode *root)
{
if(!root)return 0;
return 1+max(getDeep(root->right),getDeep(root->left));
}
};



#### 958. 二叉树的完全性检验

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isCompleteTree(TreeNode* root) {
if (!root)return true;
queue<TreeNode*>que;
que.push(root);
bool flag = true,st=true;
int ans = 1;
while (que.size())
{
int n = que.size();
st = true;
if (n != ans)flag = false;
for (int i = 0; i < n; i++)
{
if (que.front()->left != NULL)
{
if (!flag)return false;
else if (!st)return false;
que.push(que.front()->left);
}
else
{
if (que.front()->right != NULL)return false;
st = false;
}
if (que.front()->right != NULL)
{
if (!flag)return false;
que.push(que.front()->right);
}
else st = false;

que.pop();
}
ans *= 2;
}
return true;
}
};


#### 967. 连续差相同的数字

class Solution {
public:
vector<int>v;
vector<int> numsSameConsecDiff(int n, int k) {

for(int i=1;i<=9;i++)
{
if(i+k<=9)v.push_back(i);
else if(i-k>=0)v.push_back(i);
}
bfs(n,k,1);
return v;
}
void bfs(int n,int k,int u)
{
if(u==n)return ;
int num;
vector<int>ans;
unordered_map<int,int>mymap;
for(auto i:v)
{
if(i%10+k<=9)
{
num=i*10+i%10+k;
if(mymap[num]==0)
{
ans.push_back(num);
mymap[num]=1;
}

}
if(i%10-k>=0)
{
num=i*10+i%10-k;
if(mymap[num]==0)
{
ans.push_back(num);
mymap[num]=1;
}
}
}
v=ans;
bfs(n,k,u+1);
}
};


#### 994. 腐烂的橘子

class Solution {
public:
typedef pair<int,int>PII;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int n,m;
int orangesRotting(vector<vector<int>>& grid) {
n=grid.size(),m=grid[0].size();
int times=0;
queue<PII>que;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]==2)
{
que.push({i,j});
}
while(que.size())
{
int len=que.size();
for(int i=0;i<len;i++)
{
for (int j = 0; j < 4; j++)
{
int a = que.front().first + dx[j], b = que.front().second + dy[j];
if (a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == 1)
{
grid[a][b] = 2;
que.push({ a,b });
}
}
que.pop();
}
if(que.size())times++;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]==1)return -1;
return times;
}
};

03-19 33

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。