当前位置: 首页 > news >正文

荆门网站建设哪家培训机构学校好

荆门网站建设,哪家培训机构学校好,北京网站建设dqcx,青岛网站建设推广公司哪家好from: https://leetcode.cn/studyplan/top-100-liked/ bfs 具有 边权为1 的最短路性质 拓扑排序,入度 Trie树, 高效存储 字符串【见鬼,不知道为什么写错,需要掌握熟练度】 文章目录 200. 岛屿数量【dfs / bfs】994. 腐…

from: https://leetcode.cn/studyplan/top-100-liked/

bfs 具有 边权为1 的最短路性质
拓扑排序,入度
Trie树, 高效存储 字符串【见鬼,不知道为什么写错,需要掌握熟练度】


文章目录

    • 200. 岛屿数量【dfs / bfs】
    • 994. 腐烂的橘子【bfs 具有 边权为1 的最短路性质】
    • 207. 课程表【拓扑排序】
    • 208. 实现 Trie (前缀树)【模板题】

200. 岛屿数量【dfs / bfs】

dfs 写法,比较简洁

class Solution {
public:int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};int n, m;int numIslands(vector<vector<char>>& grid) {n = grid.size(), m = grid[0].size();int cnt = 0;for(int i = 0;i < n;i ++ ){for(int j = 0;j < m;j ++ ){if(grid[i][j] == '1') {cnt ++ ;dfs(i, j, grid);}}}return cnt;}void dfs(int x, int y,vector<vector<char>>& grid){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(a, b, grid);}};
};

bfs 写法,有最短路性质

#define x first
#define y secondclass Solution {
public:int n, m;typedef pair<int,int> PII;int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};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 ++;bfs(i,j,grid);}return res;}void bfs(int x,int y,vector<vector<char>>& grid){queue<PII> q;q.push({x,y});grid[x][y] = '0';while(!q.empty()){auto t = q.front();q.pop();for(int i=0;i<4;i++){int a = t.x + dx[i], b =t.y + dy[i]; // debug : 这里是新坐标的t.x 不是 xif(a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == '1'){grid[a][b] = '0';q.push({a,b});}}}}
};

994. 腐烂的橘子【bfs 具有 边权为1 的最短路性质】

bfs 具有 边权为1 的最短路性质

class Solution {
public:int orangesRotting(vector<vector<int>>& grid) {int n = grid.size(), m = grid[0].size();bool st[n][m];memset(st, 0, sizeof st);queue<pair<int,int>> q;int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};for(int i = 0;i < n;i ++ ){for(int j = 0; j < m;j ++ ){if(grid[i][j] == 2) {q.push({i, j});st[i][j] = true;}}}int res = 0;while(q.size()){int k = q.size(); // debug: int k, 写成n 和 前面命名重复了!res ++ ;while(k -- ){auto t = q.front();q.pop();for(int i = 0;i < 4;i ++ ){int a = t.first + dx[i], b = t.second + dy[i];if(a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == 1 && !st[a][b]){q.push({a, b});grid[a][b] = 2;st[a][b] = true;}}}}for(int i = 0;i < n;i ++ ){for(int j = 0; j < m;j ++ ){if(grid[i][j] == 1) {return -1;}}}if(res == 0) return 0;return res - 1;}
};

207. 课程表【拓扑排序】

拓扑排序

class Solution {
public:bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {// 拓扑排序int d[numCourses];memset(d, 0, sizeof d);vector<int> g[numCourses];for(auto &c : prerequisites) {int a = c[0], b = c[1];g[a].push_back(b);d[b] ++ ;}queue<int> q;for(int i = 0;i < numCourses;i ++ ){if(d[i] == 0) q.push(i);}while(q.size()){int t = q.front();q.pop();for(auto to : g[t]){d[to] -- ;if(d[to] == 0) q.push(to);}}for(int i = 0;i < numCourses;i ++ ){if(d[i] != 0) return false;}return true;}
};

208. 实现 Trie (前缀树)【模板题】

模板题

数组写法,简洁,需要注意开的数组空间 N * 结点

const int N = 30010;int tr[N * 26][26], idx;
int cnt[N * 26];class Trie {
public:Trie() {idx = 0;memset(tr, 0, sizeof tr);memset(cnt, 0, sizeof cnt);}void insert(string word) {int p = 0;for(auto c : word){int u = c - 'a';if(!tr[p][u]) tr[p][u] = ++ idx;p = tr[p][u];}cnt[p] ++ ;}bool search(string word) {int p = 0;for(auto c : word){int u = c - 'a';if(!tr[p][u]) return false;p = tr[p][u];}return cnt[p] > 0;}bool startsWith(string prefix) {int p = 0;for(auto c : prefix){int u = c - 'a';if(!tr[p][u]) return false;p = tr[p][u];}return true;}
};/*** Your Trie object will be instantiated and called as such:* Trie* obj = new Trie();* obj->insert(word);* bool param_2 = obj->search(word);* bool param_3 = obj->startsWith(prefix);*/

指针写法

class Trie {
public:struct Node{bool is_end;Node *son[26];Node(){is_end = false;for(int i=0;i<26;i++) son[i] = NULL;}}*root;/** Initialize your data structure here. */Trie() {root = new Node();}/** Inserts a word into the trie. */void insert(string word) {auto *p = root;for(auto c : word){int u = c - 'a';if(p->son[u] == NULL) p->son[u] = new Node();p = p->son[u];}p->is_end = true;}/** Returns if the word is in the trie. */bool search(string word) {auto *p = root;for(auto c : word){int u = c - 'a';if(p->son[u] == NULL) return false;p = p->son[u];}return p->is_end;}/** Returns if there is any word in the trie that starts with the given prefix. */bool startsWith(string prefix) {auto *p = root;for(auto c : prefix){int u = c - 'a';if(p->son[u] == NULL) return false;p = p->son[u]; }return true;}
};/*** Your Trie object will be instantiated and called as such:* Trie* obj = new Trie();* obj->insert(word);* bool param_2 = obj->search(word);* bool param_3 = obj->startsWith(prefix);*/
http://www.tj-hxxt.cn/news/35556.html

相关文章:

  • 长沙大型网络网站制作公司成都黑帽seo
  • web网站开发前后端裂变营销
  • 门户网站开发公司服务器租用
  • 养老院网站建设的费用seo泛目录培训
  • 网页设计与网站开发东莞网站建设平台
  • 17来做网站瑞昌网络推广
  • 凡科平台和教育部盲审平台区别seo推广系统排名榜
  • 网站管理助手创建数据库站长工具综合查询2020
  • 做彩网站谷歌浏览器2021最新版
  • 个人网站设计图杭州网站seo优化
  • 百度免费做网站许昌seo推广
  • 做购票系统网站写文的免费软件
  • 动态网站数据库设计代写软文费用全网天下实惠
  • 电子商务网站功能设计与分析软文类型
  • 做网站的详细流程seo排名外包
  • 海南高端网站建设网站代发外链
  • wordpress开发cms乐云seo
  • 2016网站建设报价表域名年龄对seo的影响
  • 上海招聘网官方网站五八精准恶意点击软件
  • 个人网站制作步骤公司推广文案
  • 有哪些做分析图用的地图网站最好的免费建站网站
  • 福州网站建设多少钱龙岗网站建设公司
  • 西安网站维保公司国产免费crm系统有哪些在线
  • cps推广网站广州网站设计
  • 网站做支付按流量付费吗优化模型
  • 做百度网站如何收费今日军事新闻头条视频
  • 怎样讲卖灯的网站做的好网站优化排名查询
  • 网站设计制造中国足彩网竞彩推荐
  • 如何为一个网站做app软文平台有哪些
  • 一个卖时时彩做号方法的网站今日国际新闻事件