笔试刷题BFS和DFS专题
BFS和DFS专题
- LeetCode 77 组合(DFS)
- LeetCode 104 树的最大深度(DFS)
- LeetCode 111 二叉树的最小深度(DFS)
- LeetCode 127 单词接龙(BFS)
- LeetCode 207 课程表(拓扑排序BFS)
- LeetCode 257 二叉树的所有路径
- LeetCode 279 完全平方数(BFS)
- LeetCode 130 被围绕的区域(DFS)
- LeetCode 200 岛屿的数量(DFS)
- LeetCode 542 01矩阵(BFS)
- LeetCode 543 二叉树的直径(DFS)
- LeetCode 733 图像渲染(DFS)
- LeetCode 784 字母大小写全排列(DFS)
LeetCode 77 组合(DFS)
class Solution {
public:vector<vector<int>> ans;vector<vector<int>> combine(int n, int k) {vector<int> path;dfs(n, k, 1, path);return ans;}void dfs(int n, int k, int t, vector<int> path){if(!k){ans.push_back(path);return;} for(int i = t; i <= n; i++){path.push_back(i); dfs(n, k-1, i+1, path);path.pop_back();} }
};
LeetCode 104 树的最大深度(DFS)
class Solution {
public:int maxDepth(TreeNode* root) {int ans = dfs(root); return ans; }int dfs(TreeNode* root){if(!root) return 0;return max(dfs(root->left), dfs(root->right)) + 1;}
};
LeetCode 111 二叉树的最小深度(DFS)
* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int minDepth(TreeNode* root) {if(!root) return 0;int left = minDepth(root->left);int right = minDepth(root->right);if(!left||!right) return left+right+1;return min(left, right)+1;}
};
LeetCode 127 单词接龙(BFS)
class Solution {
public:bool check(string a, string b){int res = 0;for (int i = 0; i < a.size(); i ++ ) res += a[i] != b[i];return res == 1;}int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_map<string, int>dist;queue<string> que;que.push(beginWord), dist[beginWord] = 1;while (!que.empty()){string t = que.front();que.pop();if (t == endWord) return dist[t];for (auto &word : wordList)if (check(t, word) && !dist[word]){dist[word] = dist[t] + 1;que.push(word);}}return 0;}
};
LeetCode 207 课程表(拓扑排序BFS)
class Solution {
public:bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {vector<vector<int>> graph(numCourses);vector<int> in_degree(numCourses, 0);for (int i = 0; i < prerequisites.size(); i++) {in_degree[prerequisites[i].first]++;graph[prerequisites[i].second].push_back(prerequisites[i].first);}queue<int> q;vector<bool> vis(numCourses, false);for (int i = 0; i < numCourses; i++)if (in_degree[i] == 0)q.push(i);while (!q.empty()) {int sta = q.front();q.pop();vis[sta] = true;for (int i = 0; i < graph[sta].size(); i++) {in_degree[graph[sta][i]]--;if (in_degree[graph[sta][i]] == 0)q.push(graph[sta][i]);}}for (int i = 0; i < numCourses; i++)if (vis[i] == false)return false;return true;}
};
LeetCode 257 二叉树的所有路径
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:vector<string> ans;vector<string> binaryTreePaths(TreeNode* root) {string path;dfs(root, path);return ans; }void dfs(TreeNode* root, string path){if(!root) return;if(!root->left && !root->right) {ans.push_back(path+to_string(root->val));return;}if(root->left) dfs(root->left, path+to_string(root->val)+"->");if(root->right) dfs(root->right, path+to_string(root->val)+"->");}
};
LeetCode 279 完全平方数(BFS)
class Solution {
public:int numSquares(int n) {queue<int> q;vector<int> dist(n+1, -1);q.push(0);dist[0]=0;while (q.size()){int t = q.front();q.pop();if(t==n) return dist[t];for(int i = 1; i*i+t<=n; i++){int j = t+i*i;if(dist[j] == -1){dist[j] = dist[t] + 1;q.push(j);}}}return 0;}
};
LeetCode 130 被围绕的区域(DFS)
class Solution {
public:vector<vector<bool>> st;int n, m;void solve(vector<vector<char>>& board) {if(board.empty() || board[0].empty()) return;n = board.size(), m = board[0].size();for(int i = 0; i<n; i++){vector<bool> temp;for(int j = 0; j<m; j++)temp.push_back(false);st.push_back(temp); }for(int i = 0; i < n; i++){if(board[i][0] == 'O') dfs(board, i, 0);if(board[i][m-1] == 'O') dfs(board, i, m-1);}for(int i = 0; i < m; i++){if(board[0][i] == 'O') dfs(board, 0, i);if(board[n-1][i] == 'O') dfs(board, n-1, i); }for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)if(!st[i][j])board[i][j] = 'X';}void dfs(vector<vector<char>>&board, int x, int y){st[x][y] = true;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};for(int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if(a >= 0 && a < n && b >= 0 && b < m && !st[a][b] && board[a][b] == 'O')dfs(board, a, b); } }
};
LeetCode 200 岛屿的数量(DFS)
class Solution {
public:int n, m;int numIslands(vector<vector<char>>& grid) {if(grid.empty() || grid[0].empty()) return 0;n = grid.size(), m = grid[0].size();int res = 0;for(int i = 0; i < n; i ++ ){for(int j = 0; j < m; j++){if(grid[i][j] == '1'){res++;dfs(grid,i,j);} } }return res; }void dfs(vector<vector<char>>& grid, int x, int y){int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};grid[x][y] = '0';for(int i = 0; i < 4; i++){int a = x + dx[i], b = y + dy[i];if(a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == '1')dfs(grid, a, b);} }
};
LeetCode 542 01矩阵(BFS)
class Solution {
public:int dx[4] = {0, 1, 0, -1};int dy[4] = {1, 0, -1, 0};vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {int n = matrix.size(), m = matrix[0].size();queue<pair<int, int>> q;vector<vector<int>> dis(n, vector<int>(m, -1));for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (matrix[i][j] == 0) {dis[i][j] = 0;q.push(make_pair(i, j));}while (!q.empty()) {pair<int, int> sta = q.front();q.pop();for (int i = 0; i < 4; i++) {int x = sta.first + dx[i], y = sta.second + dy[i];if (x < 0 || x >= n || y < 0 || y >= m || dis[x][y] != -1)continue;dis[x][y] = dis[sta.first][sta.second] + 1;q.push(make_pair(x, y));}}return dis;}
};
LeetCode 543 二叉树的直径(DFS)
class Solution {
public:int diameterOfBinaryTree(TreeNode* root) {int ans = 0;dfs(root, ans);return ans;}int dfs(TreeNode *r, int &ans){if(r == NULL) return -1;int d1 = dfs(r->left, ans);int d2 = dfs(r->right, ans);ans = max(ans, d1+d2+2);return max(d1, d2)+1;}
};
LeetCode 733 图像渲染(DFS)
class Solution {
public:vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {if(image.empty()||image[0].empty()) return image;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};int oldColor = image[sr][sc];if(oldColor == newColor) return image;image[sr][sc] = newColor;for(int i = 0; i<4; i++ ){int x = sr + dx[i], y = sc + dy[i];if(x >= 0 && x < image.size() && y >= 0 && y < image[0].size() && image[x][y] == oldColor)floodFill(image, x, y, newColor);}return image;}
};
LeetCode 784 字母大小写全排列(DFS)
class Solution {
public:vector<string> ans;vector<string> letterCasePermutation(string S) {dfs(S, 0);return ans;}void dfs(string S, int t){if(t==S.size()) {ans.push_back(S); return; } dfs(S, t+1);if(S[t]>='A'){S[t]^=32;dfs(S, t+1);}}
};
笔试刷题BFS和DFS专题相关推荐
- 刷题 BFS 广度优先算法 : 大胖子走迷宫 (python, java)
刷题 BFS 广度优先算法 : 大胖子走迷宫 (python, java) https://www.lanqiao.cn/problems/234/learning/ http://lx.lanqia ...
- Leecode刷题【1数组专题4】80. 删除排序数组中的重复项II (以及通用解法)
Leecode刷题 [1数组专题4]80. 删除排序数组中的重复项II (以及通用解法) 题目: 思路 双指针法: (错误代码) 正解: 通用解法: 题目: 给你一个有序数组 nums ,请你 原地 ...
- 《学Unity的猫》——第十八集:Unity3D游戏开发工程师笔试刷题,皮皮收到面试邀请
文章目录 18.1 皮皮收到面试邀请 18.2 面试题库相关网站 18.2.1 牛客网 18.2.2 领扣LintCode 18.2.3 力扣LeetCode 18.3 优质学习网站 18.3.1 菜 ...
- 软通动力华为java机考题库_华为机考笔试刷题-java-1
题库来源 计算字符个数 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数.不区分大小写. public static void main(String[ ...
- 【笔试刷题训练】day_04
选择题 C/C++中各种进制的表示方法 二进制:在数字的末尾加b,如101010b 八进制:在数字前面加数字0,如0123 十进制:数字本身,如123 十六进制:数字前面加0x 或者 数字后面加h,如 ...
- LeetCode笔试刷题一. 贪心算法
一.算法解释 贪心算法或贪心思想采用贪心的策略,保证每次操作都是局部最优的,从而使最后得到的结果是全局最优的. 二.题型 2.1 分配问题 455. 分发饼干 饥饿度最小的最容易吃饱,将饥饿度从小到大 ...
- 产品笔试刷题错题记录
目录 2021-07-01 2021-07-02 2021-07-03 2021-07-05 2021-07-09 2021-07-10 2021-07-01 题目一.下面哪一项不属于做竞品分析的方法 ...
- acm刷题一些总结,至每一个努力拼搏的acmer
17蒟蒻转眼或成退役老学叔 真正接触acm是在大二上学期,大一学校安排的课程程序设计基础主要是写类似于pascal的伪代码,自己也缺乏了解的渠道,对拿奖拿到手软的大佬充满羡慕.直到大二刚开学,我参加了 ...
- 刷题笔记(十四)--二叉树:层序遍历和DFS,BFS
目录 系列文章目录 前言 题录 102. 二叉树的层序遍历 BFS DFS_前序遍历 107. 二叉树的层序遍历 II BFS DFS 199. 二叉树的右视图 BFS DFS 637. 二叉树的层平 ...
- leetcode之DFS+BFS+DSU刷题总结2
leetcode之DFS+BFS+DSU刷题总结2 1-对称二叉树 题目链接:题目链接戳这里!!! 思路1:迭代法 一棵二叉树,左右子树分别进队,如果左右子树都为空,则结束遍历,如果左右子树仅一个为空 ...
最新文章
- hive动态分区shell_Hive动态分区 参数配置及语法
- 网络推广网站浅析如何做好关键词布局优化?
- finereport 登录界面的代码文件_【干货下载】多彩包含网页登录界面等4款WEB模板素材作品集源文件...
- bilibili源码_selenium+phantomjs爬取bilibili
- 我和学员那些事儿——涅槃重生的背后
- 【H.264/AVC视频编解码技术】第三章【熵编码】
- 滴滴 KDD 2018 论文详解:基于强化学习技术的智能派单模型
- 电脑入门完全自学手册_电气自动化自学宝典——看过人人都是工程师
- python虚拟环境安装包_Python虚拟环境的创建和包下载过程分析
- 用了这么久,你真的真的明白 HttpClient 的实现原理了吗?
- apache的源码包编译
- nekohtml资料
- 【单片机学习笔记】上传一整年的自学电子笔记,互相交流,共同进步。
- Python每日一练——第5天:闰年问题升级版
- 2021年中国移动广告行业发展现状及趋势:发展趋势不断攀升,互动广告已逐渐成为主流[图]
- noip2011 公交观光
- Python开发培训哪里好
- SQL Server 创建和管理数据表
- lap 加MySQL主从复制_LAP+mysql-主从+redis
- Elasticsearch学习笔记:MUST_NOT not working with EXIST in NESTED query