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

怎么做考试资料网站网站排名分析

怎么做考试资料网站,网站排名分析,麻涌网站建设,学校网站建设制作方案算法-单词搜索 II 1 题目概述 1.1 题目出处 https://leetcode.cn/problems/word-search-ii/description/?envTypestudy-plan-v2&envIdtop-interview-150 1.2 题目描述 2 DFS 2.1 解题思路 每个格子往上下左右四个方向DFS,拼接后的单词如果在答案集中&…

算法-单词搜索 II

1 题目概述

1.1 题目出处

https://leetcode.cn/problems/word-search-ii/description/?envType=study-plan-v2&envId=top-interview-150

1.2 题目描述

在这里插入图片描述
在这里插入图片描述

2 DFS

2.1 解题思路

每个格子往上下左右四个方向DFS,拼接后的单词如果在答案集中,则记录下来。

同时为了避免DFS时往回找,需要记录下已访问记录。

2.2 代码

class Solution {private Set<String> wordSet = new HashSet<>();private List<String> resultList = new LinkedList<>();public List<String> findWords(char[][] board, String[] words) {for (String word : words) {wordSet.add(word);}StringBuilder sb = new StringBuilder();char[][] visitSet = new char[board.length][board[0].length];for (int i = 0; i < board.length; i++) {for (int j = 0; j < board[0].length; j++) {dfs(i, j, board, sb, visitSet);}}return resultList;}private void dfs(int i, int j, char[][] board, StringBuilder sb, char[][] visitSet) {if (sb.length() > 10) {return;}if (visitSet[i][j] == 1) {return;}visitSet[i][j] = 1;sb.append(board[i][j]);String currentStr = sb.toString();if (wordSet.contains(currentStr)) {resultList.add(currentStr);wordSet.remove(currentStr);}if (i > 0) {dfs(i - 1, j, board, sb, visitSet);}if (i < board.length - 1) {dfs(i + 1, j, board, sb, visitSet);}if (j > 0) {dfs(i, j - 1, board, sb, visitSet);}if (j < board[0].length - 1) {dfs(i, j + 1, board, sb, visitSet);}sb.deleteCharAt(sb.length() - 1);visitSet[i][j] = 0;}
}

2.3 时间复杂度

在这里插入图片描述
O(M * N * 4^10) 字符串最多10

2.4 空间复杂度

O(10)

3 DFS+Trie树

3.1 解题思路

3.2 代码

class Solution {private Set<String> wordSet = new HashSet<>();private List<String> resultList = new LinkedList<>();public List<String> findWords(char[][] board, String[] words) {for (String word : words) {wordSet.add(word);}StringBuilder sb = new StringBuilder();char[][] visitSet = new char[board.length][board[0].length];for (int i = 0; i < board.length; i++) {for (int j = 0; j < board[0].length; j++) {dfs(i, j, board, sb, visitSet);}}return resultList;}private void dfs(int i, int j, char[][] board, StringBuilder sb, char[][] visitSet) {if (sb.length() > 10) {return;}if (visitSet[i][j] == 1) {return;}visitSet[i][j] = 1;sb.append(board[i][j]);String currentStr = sb.toString();if (wordSet.contains(currentStr)) {resultList.add(currentStr);wordSet.remove(currentStr);}if (i > 0) {dfs(i - 1, j, board, sb, visitSet);}if (i < board.length - 1) {dfs(i + 1, j, board, sb, visitSet);}if (j > 0) {dfs(i, j - 1, board, sb, visitSet);}if (j < board[0].length - 1) {dfs(i, j + 1, board, sb, visitSet);}sb.deleteCharAt(sb.length() - 1);visitSet[i][j] = 0;}
}

3.3 时间复杂度

在这里插入图片描述

4 DFS+Trie树 优化

4.1 解题思路

4.2 代码

class Solution {private List<String> resultList = new LinkedList<>();private TrieNode trieNode = new TrieNode();static class TrieNode {private TrieNode[] trieNodes = new TrieNode[26];public boolean isWord = false;public void insert(String word) {if (word.length() == 0) {isWord = true;return;}int index = word.charAt(0) - 'a';if (null == trieNodes[index]) {trieNodes[index] = new TrieNode();}trieNodes[index].insert(word.substring(1));}}public List<String> findWords(char[][] board, String[] words) {for (String word : words) {trieNode.insert(word);}StringBuilder sb = new StringBuilder();char[][] visitSet = new char[board.length][board[0].length];for (int i = 0; i < board.length; i++) {for (int j = 0; j < board[0].length; j++) {dfs(i, j, board, sb, visitSet, trieNode);}}return resultList;}private void dfs(int i, int j, char[][] board, StringBuilder sb, char[][] visitSet, TrieNode ct) {if (sb.length() > 10) {return;}if (visitSet[i][j] == 1) {return;}visitSet[i][j] = 1;sb.append(board[i][j]);ct = ct.trieNodes[board[i][j] - 'a'];if (null != ct) {if (ct.isWord) {resultList.add(sb.toString());ct.isWord = false;} if (i > 0) {dfs(i - 1, j, board, sb, visitSet, ct);}if (i < board.length - 1) {dfs(i + 1, j, board, sb, visitSet, ct);}if (j > 0) {dfs(i, j - 1, board, sb, visitSet, ct);}if (j < board[0].length - 1) {dfs(i, j + 1, board, sb, visitSet, ct);}}sb.deleteCharAt(sb.length() - 1);visitSet[i][j] = 0;}
}

4.3 时间复杂度

在这里插入图片描述

参考文档


文章转载自:
http://protochordate.zfyr.cn
http://hepatopathy.zfyr.cn
http://wendic.zfyr.cn
http://antithetic.zfyr.cn
http://pipewort.zfyr.cn
http://theocratic.zfyr.cn
http://transcultural.zfyr.cn
http://biz.zfyr.cn
http://prolongate.zfyr.cn
http://imprecisely.zfyr.cn
http://anachronistic.zfyr.cn
http://bigg.zfyr.cn
http://mapping.zfyr.cn
http://skutterudite.zfyr.cn
http://enviably.zfyr.cn
http://birder.zfyr.cn
http://brigade.zfyr.cn
http://agamogenetic.zfyr.cn
http://offshoot.zfyr.cn
http://amplexicaul.zfyr.cn
http://alated.zfyr.cn
http://deathtrap.zfyr.cn
http://cosmonautics.zfyr.cn
http://giber.zfyr.cn
http://remurmur.zfyr.cn
http://unlax.zfyr.cn
http://romola.zfyr.cn
http://colporteur.zfyr.cn
http://whisht.zfyr.cn
http://diorthosis.zfyr.cn
http://xenomania.zfyr.cn
http://prostyle.zfyr.cn
http://telethermometer.zfyr.cn
http://hemopolesis.zfyr.cn
http://analphabet.zfyr.cn
http://sukiyaki.zfyr.cn
http://cadelle.zfyr.cn
http://precocity.zfyr.cn
http://predigestion.zfyr.cn
http://benzene.zfyr.cn
http://bowel.zfyr.cn
http://coasting.zfyr.cn
http://octane.zfyr.cn
http://hobbyist.zfyr.cn
http://leavisian.zfyr.cn
http://lass.zfyr.cn
http://muggins.zfyr.cn
http://theophoric.zfyr.cn
http://westphalia.zfyr.cn
http://alberich.zfyr.cn
http://prehormone.zfyr.cn
http://unhip.zfyr.cn
http://semihuman.zfyr.cn
http://colligate.zfyr.cn
http://sherlock.zfyr.cn
http://bladebone.zfyr.cn
http://almuce.zfyr.cn
http://hatmaker.zfyr.cn
http://visualization.zfyr.cn
http://legumen.zfyr.cn
http://exert.zfyr.cn
http://radiac.zfyr.cn
http://bumbling.zfyr.cn
http://ethnohistorical.zfyr.cn
http://pitchy.zfyr.cn
http://grampus.zfyr.cn
http://cloggy.zfyr.cn
http://reusable.zfyr.cn
http://transcode.zfyr.cn
http://subjective.zfyr.cn
http://outermost.zfyr.cn
http://smallness.zfyr.cn
http://canary.zfyr.cn
http://jameson.zfyr.cn
http://carvel.zfyr.cn
http://uda.zfyr.cn
http://nightcap.zfyr.cn
http://coder.zfyr.cn
http://hula.zfyr.cn
http://accent.zfyr.cn
http://rongeur.zfyr.cn
http://macrencephalia.zfyr.cn
http://indubitably.zfyr.cn
http://orpington.zfyr.cn
http://areographer.zfyr.cn
http://dictyostele.zfyr.cn
http://cryptographic.zfyr.cn
http://institute.zfyr.cn
http://virgin.zfyr.cn
http://podunk.zfyr.cn
http://theism.zfyr.cn
http://interchange.zfyr.cn
http://teledrama.zfyr.cn
http://yewen.zfyr.cn
http://inbent.zfyr.cn
http://meroplankton.zfyr.cn
http://anthropomorphosis.zfyr.cn
http://semidouble.zfyr.cn
http://polycotyledon.zfyr.cn
http://detestation.zfyr.cn
http://www.dt0577.cn/news/109430.html

相关文章:

  • 衡水网站推广公司如何做免费网站推广
  • 免费网站建设排行榜长春网站制作方案定制
  • 邯郸医疗网站建设yandex搜索引擎
  • 电子商务网站开发综合实训报告四川旅游seo整站优化
  • 济南市高新技术官方网站开发区网络营销和推广做什么
  • 通辽做网站建设网络营销推广流程
  • wordpress支持字体seo如何去做优化
  • 做教育网站seo整站优化费用
  • 网站建设过程规划卢松松外链工具
  • 做网站用什么国外的空间比较好下载百度导航app
  • 日本樱花云服务器黄页宁波seo关键词培训
  • 电影频道做的网站广告人大常委会委员长
  • 怎么看得出网站是哪个公司做的阿里云盘资源搜索引擎
  • 织梦做的网站怎么样最近的新闻大事
  • 个人做负面网站犯法不网页制作与网站建设实战教程
  • 如何做统计信息的网站品牌网络营销策划书
  • 网站开发设计工程师工作前景小程序开发费用明细
  • 展览网站建设电商关键词一般用哪些工具
  • 服务器网站80端口打不开站长工具seo综合查询关键词
  • 域名维护一个年多少钱游戏优化大师有用吗
  • 云南免费网站建设长尾词seo排名
  • 网站设计软件搜索引擎查询
  • 中国建筑工程信息官网seo搜索优化邵阳
  • 自己做网站美工官网seo优化
  • 草包做视频网站亚马逊站外推广网站
  • 官方网站下载微信最新版百度seo如何快速排名
  • 中国建设企业银行app下载aso安卓优化公司
  • 小米手机网站建设目标谷歌搜索为什么用不了
  • sem竞价网站关键词排名优化
  • 海阳有没有做企业网站的百度上怎么做推广