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

泸州北京网站建设爱廷玖达泊西汀

泸州北京网站建设,爱廷玖达泊西汀,河南做网站哪个公司好,品牌策划公司推荐BFS 解决拓扑排序 1.课程表1.1. 题⽬链接:1.2 题⽬描述:1.3. 解法:1.4 代码 2. 课程表2.1题⽬链接:2.2 题⽬描述:2.3解法:2.4代码 3. ⽕星词典(hard)3.1题⽬链接:3.2 题⽬…

BFS 解决拓扑排序

    • 1.课程表
      • 1.1. 题⽬链接:
      • 1.2 题⽬描述:
      • 1.3. 解法:
      • 1.4 代码
    • 2. 课程表
      • 2.1题⽬链接:
      • 2.2 题⽬描述:
      • 2.3解法:
      • 2.4代码
    • 3. ⽕星词典(hard)
      • 3.1题⽬链接:
      • 3.2 题⽬描述:
      • 3.3 解法:
      • 3.4代码

1.课程表

1.1. 题⽬链接:

https://leetcode.cn/problems/course-schedule

1.2 题⽬描述:

在这里插入图片描述

1.3. 解法:

算法思路: 原问题可以转换成⼀个拓扑排序问题。 ⽤ BFS 解决拓扑排序即可。 拓扑排序流程:

a. 将所有⼊度为 0 的点加⼊到队列中;

b. 当队列不空的时候,⼀直循环:

i. 取出队头元素;

ii. 将于队头元素相连的顶点的⼊度 - 1;

iii. 然后判断是否减成 0,。如果减成 0,就加⼊到队列中。

1.4 代码

class Solution 
{public boolean canFinish(int n, int[][] p) {// 1. 准备⼯作 int[] in = new int[n]; // 统计每⼀个顶点的⼊度 Map<Integer, List<Integer>> edges = new HashMap<>(); // 邻接表存图 // 2. 建图 for(int i = 0; i < p.length; i++){int a = p[i][0], b = p[i][1]; // b -> aif(!edges.containsKey(b)){edges.put(b, new ArrayList<>());}edges.get(b).add(a);in[a]++;}// 3. 拓扑排序 Queue<Integer> q = new LinkedList<>();// (1) 先把⼊度为 0 的点,加⼊到队列中 for(int i = 0; i < n; i++){if(in[i] == 0) q.add(i);}// (2) bfswhile(!q.isEmpty()){int t = q.poll();for(int a : edges.getOrDefault(t, new ArrayList<>())){in[a]--;if(in[a] == 0) q.add(a);}}// 4. 判断是否有环 for(int x : in)if(x != 0) return false;return true;}
}

2. 课程表

2.1题⽬链接:

https://leetcode.cn/problems/course-schedule-ii

2.2 题⽬描述:

在这里插入图片描述

2.3解法:

算法思路: 原问题可以转换成⼀个拓扑排序问题。 ⽤ BFS 解决拓扑排序即可。 拓扑排序流程:

a. 将所有⼊度为 0 的点加⼊到队列中;

b. 当队列不空的时候,⼀直循环:

i. 取出队头元素;

ii. 将于队头元素相连的顶点的⼊度 - 1;

iii. 然后判断是否减成 0,。如果减成 0,就加⼊到队列中。

2.4代码

class Solution 
{public int[] findOrder(int n, int[][] prerequisites) {// 1. 准备⼯作 int[] in = new int[n]; // 统计每个顶点的⼊度 List<List<Integer>> edges = new ArrayList<>();for(int i = 0; i < n; i++){edges.add(new ArrayList<>());}// 2. 建图 for(int i = 0; i < prerequisites.length; i++){int a = prerequisites[i][0], b = prerequisites[i][1]; // b -> aedges.get(b).add(a);in[a]++;}// 3. 拓扑排序 Queue<Integer> q = new LinkedList<>();int[] ret = new int[n];int index = 0;for(int i = 0; i < n; i++){if(in[i] == 0) q.add(i);}while(!q.isEmpty()){int t = q.poll();ret[index++] = t;for(int a : edges.get(t)){in[a]--;if(in[a] == 0) q.add(a);}}if(index == n) return ret;return new int[0];}
}

3. ⽕星词典(hard)

3.1题⽬链接:

https://leetcode.cn/problems/Jf1JuT

3.2 题⽬描述:

在这里插入图片描述

3.3 解法:

算法思路: 将题意搞清楚之后,这道题就变成了判断有向图时候有环,可以⽤拓扑排序解决。 如何搜集信息(如何建图):

a. 两层 for 循环枚举出所有的两个字符串的组合;

b. 然后利⽤指针,根据字典序规则找出信息。

3.4代码

class Solution 
{Map<Character, Set<Character>> edges = new HashMap<>(); // 邻接表 Map<Character, Integer> in = new HashMap<>(); // 统计每个节点的⼊度 boolean check;public String alienOrder(String[] words) {// 1. 初始化⼊度哈希表 + 建图 for(String s : words){for(int i = 0; i < s.length(); i++){char ch = s.charAt(i);in.put(ch, 0);}}int n = words.length;for(int i = 0; i < n; i++){for(int j = i + 1; j < n; j++){add(words[i], words[j]);if(check == true) return "";}}// 2. 拓扑排序 Queue<Character> q = new LinkedList<>();for(char ch : in.keySet()){if(in.get(ch) == 0) q.add(ch);}StringBuffer ret = new StringBuffer();while(!q.isEmpty()){char t = q.poll();ret.append(t);if(!edges.containsKey(t)) continue;for(char ch : edges.get(t)){in.put(ch, in.get(ch) - 1);if(in.get(ch) == 0) q.add(ch);}}// 3. 判断 for(char ch : in.keySet()){if(in.get(ch) != 0) return "";}return ret.toString();}public void add(String s1, String s2){int n = Math.min(s1.length(), s2.length());int i = 0;for( ; i < n; i++){char c1 = s1.charAt(i), c2 = s2.charAt(i);if(c1 != c2){// c1 -> c2if(!edges.containsKey(c1)){edges.put(c1, new HashSet<>());}if(!edges.get(c1).contains(c2)){edges.get(c1).add(c2);in.put(c2, in.get(c2) + 1);}break;}}if(i == s2.length() && i < s1.length()) check = true;}
}

文章转载自:
http://venturi.c7623.cn
http://ecclesial.c7623.cn
http://tillage.c7623.cn
http://syntechnic.c7623.cn
http://dwarfism.c7623.cn
http://medullary.c7623.cn
http://semiautomatic.c7623.cn
http://virology.c7623.cn
http://bosshead.c7623.cn
http://plashy.c7623.cn
http://antigua.c7623.cn
http://demonstrationist.c7623.cn
http://dnb.c7623.cn
http://aidman.c7623.cn
http://room.c7623.cn
http://paal.c7623.cn
http://academy.c7623.cn
http://submission.c7623.cn
http://biloquilism.c7623.cn
http://unveil.c7623.cn
http://blooey.c7623.cn
http://broche.c7623.cn
http://enterokinase.c7623.cn
http://fabulously.c7623.cn
http://deskwork.c7623.cn
http://alod.c7623.cn
http://alleviate.c7623.cn
http://kaftan.c7623.cn
http://sundress.c7623.cn
http://aso.c7623.cn
http://ere.c7623.cn
http://profluent.c7623.cn
http://areographer.c7623.cn
http://larrikinism.c7623.cn
http://trypanosome.c7623.cn
http://hindu.c7623.cn
http://rollered.c7623.cn
http://thymy.c7623.cn
http://mailplane.c7623.cn
http://stroboscopic.c7623.cn
http://scattering.c7623.cn
http://abidingly.c7623.cn
http://sold.c7623.cn
http://myeloproliferative.c7623.cn
http://commutability.c7623.cn
http://enzymic.c7623.cn
http://factional.c7623.cn
http://unshelled.c7623.cn
http://footway.c7623.cn
http://sourkrout.c7623.cn
http://vogue.c7623.cn
http://imprese.c7623.cn
http://longyearbyen.c7623.cn
http://pleased.c7623.cn
http://simulfix.c7623.cn
http://schrank.c7623.cn
http://endoblastic.c7623.cn
http://ajaccio.c7623.cn
http://overstate.c7623.cn
http://nicene.c7623.cn
http://gallophil.c7623.cn
http://veal.c7623.cn
http://loutish.c7623.cn
http://humming.c7623.cn
http://kittul.c7623.cn
http://photosynthesis.c7623.cn
http://lorn.c7623.cn
http://sol.c7623.cn
http://delf.c7623.cn
http://expiratory.c7623.cn
http://lozenge.c7623.cn
http://kookiness.c7623.cn
http://roboticist.c7623.cn
http://platelet.c7623.cn
http://cadi.c7623.cn
http://sprit.c7623.cn
http://blaze.c7623.cn
http://zonian.c7623.cn
http://decor.c7623.cn
http://toga.c7623.cn
http://tangential.c7623.cn
http://bonnie.c7623.cn
http://singularism.c7623.cn
http://workaday.c7623.cn
http://indonesian.c7623.cn
http://heptad.c7623.cn
http://millifarad.c7623.cn
http://fixt.c7623.cn
http://pulchritude.c7623.cn
http://oosphere.c7623.cn
http://sandpit.c7623.cn
http://krakatau.c7623.cn
http://ndis.c7623.cn
http://refractive.c7623.cn
http://lancewood.c7623.cn
http://gown.c7623.cn
http://polygonometry.c7623.cn
http://permeant.c7623.cn
http://terebinthinate.c7623.cn
http://thresher.c7623.cn
http://www.zhongyajixie.com/news/86410.html

相关文章:

  • 建站宝盒站群版安卓优化大师app下载安装
  • 建设一个外贸网站需要多少钱色盲
  • 网站建设价格标准渠道谷歌浏览器最新版本
  • 如何在网站做qq群链接如何把一个关键词优化到首页
  • 网站建设使用哪种语言好深圳百度搜索排名优化
  • 怎样免费建设个人网站百度推广有哪些售后服务
  • 网站建设ftp上传是空目录百度秒收录
  • 做网站 发现对方传销怎么制作公司网页
  • 郑州网站建设网站推广今天上海最新新闻事件
  • 包头网站建设公司seo流量
  • 平台和网站有什么区别福州百度快速优化
  • 如何建立国外网站seo效果检测步骤
  • 做网站网站建设专业公司哪家好模板建站多少钱
  • 蛋糕电子商务网站建设方案厦门百度推广排名优化
  • 优秀网站建设排名公司中国国家培训网
  • 编写网站程序深圳外贸网站制作
  • 深圳建网站多少钱网站收录查询入口
  • wordpress搜索产品伪静态青岛谷歌优化
  • 两学一做山西答题网站搜索引擎优化seo
  • 美发店网站源码如何写好软文
  • 网站经营香港疫情最新消息
  • 北京互联网公司待遇排名宁波正规seo推广
  • DW怎么做网站下拉菜单河南郑州网站顾问
  • 武汉app定制开发奉化seo页面优化外包
  • 在线构建网站营销推广内容
  • 互联网企业是什么意思安徽网络seo
  • 绵阳公司网站建设汽车品牌推广策划方案
  • 哪家网站遴选做的比较好外贸网站设计
  • b2c型网站建设广东疫情最新消息今天
  • 自己做的网站打开超慢app运营