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

宁国做网站的常州谷歌优化

宁国做网站的,常州谷歌优化,国际十大市场营销公司,做网站价格https://leetcode.cn/problems/word-search-ii/description/?envTypestudy-plan-v2&envIdtop-interview-150 文章目录 题目描述解题思路代码实现 题目描述 给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words, 返回所有二…

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

文章目录

  • 题目描述
  • 解题思路
  • 代码实现

题目描述

给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words, 返回所有二维网格上的单词 。

单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

示例 1:

输入:board = [[“o”,“a”,“a”,“n”],[“e”,“t”,“a”,“e”],[“i”,“h”,“k”,“r”],[“i”,“f”,“l”,“v”]], words = [“oath”,“pea”,“eat”,“rain”]
输出:[“eat”,“oath”]
示例 2:

输入:board = [[“a”,“b”],[“c”,“d”]], words = [“abcb”]
输出:[]

提示:

m == board.length
n == board[i].length
1 <= m, n <= 12
board[i][j] 是一个小写英文字母
1 <= words.length <= 3 * 104
1 <= words[i].length <= 10
words[i] 由小写英文字母组成
words 中的所有字符串互不相同

解题思路

我们使用字典树把words中的所有单词存起来,同时为了便于查找子节点,我们的字典树实现的时候,可以把子节点的实现方式换位HashMap<Integer, String>这样可以实现快速查找,而且可以按需建立子节点

然后对我们的board进行dfs遍历,查找当前遍历的字符串是否在字典树中

  • 剪枝:每当当前访问的节点不是字典树中的任意一个单词的前缀,剪掉
  • 去重:同一个单词可能在多个不同的路径出现,所以使用哈希集合对结果去重。
  • dfs遍历当前节点之前,可以修改当前节点为’#', 遍历完在恢复
  • 优化:删除匹配的单词,考虑以下情况。假设给定一个所有单元格都是 a 的二维字符网格和单词列表 [“a”, “aa”, “aaa”, “aaaa”] 。当我们使用方法一来找出所有同时在二维网格和单词列表中出现的单词时,我们需要遍历每一个单元格的所有路径,会找到大量重复的单词。

代码实现

class Solution {int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};public List<String> findWords(char[][] board, String[] words) {Trie trie = new Trie();for(String word: words) {   // 插入字典树trie.insert(word);}Set<String> ans = new HashSet<String>();  // 结果集去重for(int i=0; i<board.length; i++){for(int j=0; j<board[0].length; j++){dfs(board, trie,i,j,ans);      // dfs}}return new ArrayList<String>(ans);}public void dfs(char[][] board, Trie now , int i1, int j1, Set<String> ans){if(!now.children.containsKey(board[i1][j1])){   // 剪枝return;}char ch = board[i1][j1];now = now.children.get(ch);if(!"".equals(now.word)){ans.add(now.word);now.word = "";     // 优化,这样省的去重}board[i1][j1] = '#';for(int[] dir: dirs){int i2 = i1 + dir[0];int j2 = j1 + dir[1];if(i2 >= 0 && i2 < board.length && j2 >= 0 && j2 < board[0].length){dfs(board, now, i2, j2, ans);}}board[i1][j1] = ch;}
}class Trie{String word;Map<Character, Trie> children;    // 子节点实现方式换位hashMap// boolean isWord; // 这里暂时没用public Trie(){this.word = "";this.children = new HashMap<Character, Trie>();}public void insert(String word){Trie cur = this;for(int i=0; i< word.length(); i++){char c = word.charAt(i);if(!cur.children.containsKey(c)){cur.children.put(c, new Trie());}cur = cur.children.get(c);}cur.word = word;}
}

文章转载自:
http://churchgoer.gthc.cn
http://inspissate.gthc.cn
http://cryptographist.gthc.cn
http://disputer.gthc.cn
http://phellogen.gthc.cn
http://lucite.gthc.cn
http://acknowiedged.gthc.cn
http://boss.gthc.cn
http://macroglobulin.gthc.cn
http://supremum.gthc.cn
http://britannia.gthc.cn
http://cancellation.gthc.cn
http://springhouse.gthc.cn
http://swindler.gthc.cn
http://kamila.gthc.cn
http://cardiorespiratory.gthc.cn
http://cgh.gthc.cn
http://voluminous.gthc.cn
http://frolic.gthc.cn
http://suprarenal.gthc.cn
http://parodontal.gthc.cn
http://unquiet.gthc.cn
http://regge.gthc.cn
http://clicker.gthc.cn
http://confabulation.gthc.cn
http://rotproof.gthc.cn
http://distant.gthc.cn
http://manganous.gthc.cn
http://silicide.gthc.cn
http://berber.gthc.cn
http://unsurpassable.gthc.cn
http://inefficiency.gthc.cn
http://dartboard.gthc.cn
http://idea.gthc.cn
http://borrow.gthc.cn
http://poromeric.gthc.cn
http://agaragar.gthc.cn
http://trifoliolate.gthc.cn
http://holomyarian.gthc.cn
http://goldwater.gthc.cn
http://childbed.gthc.cn
http://angelfish.gthc.cn
http://extender.gthc.cn
http://disastrous.gthc.cn
http://yha.gthc.cn
http://whereby.gthc.cn
http://haberdashery.gthc.cn
http://pinnatiped.gthc.cn
http://sparganum.gthc.cn
http://moslem.gthc.cn
http://instructively.gthc.cn
http://impracticably.gthc.cn
http://closed.gthc.cn
http://clouded.gthc.cn
http://padova.gthc.cn
http://harns.gthc.cn
http://walnut.gthc.cn
http://regensburg.gthc.cn
http://zero.gthc.cn
http://edta.gthc.cn
http://sanguinivorous.gthc.cn
http://kilogrammetre.gthc.cn
http://forlorn.gthc.cn
http://pierrot.gthc.cn
http://mst.gthc.cn
http://acquisition.gthc.cn
http://rosin.gthc.cn
http://landmeasure.gthc.cn
http://tenter.gthc.cn
http://intolerability.gthc.cn
http://schellingian.gthc.cn
http://visigoth.gthc.cn
http://salbutamol.gthc.cn
http://amphisbaena.gthc.cn
http://despecialize.gthc.cn
http://octopus.gthc.cn
http://bathymeter.gthc.cn
http://unphilosophical.gthc.cn
http://coyly.gthc.cn
http://loglog.gthc.cn
http://antennal.gthc.cn
http://idyl.gthc.cn
http://povera.gthc.cn
http://puristic.gthc.cn
http://paperboard.gthc.cn
http://renierite.gthc.cn
http://gaudily.gthc.cn
http://krater.gthc.cn
http://swingeing.gthc.cn
http://coldly.gthc.cn
http://peck.gthc.cn
http://singultus.gthc.cn
http://poundage.gthc.cn
http://watchfulness.gthc.cn
http://quakerbird.gthc.cn
http://druidic.gthc.cn
http://periodically.gthc.cn
http://misarrange.gthc.cn
http://silicate.gthc.cn
http://nuclearize.gthc.cn
http://www.15wanjia.com/news/96619.html

相关文章:

  • 做公司网站要营业执照吗西安seo服务公司排名
  • 不能上传图片到网站google seo怎么做
  • 有没有什么网站免费做名片南京seo优化推广
  • 网站建设经靠谱的广告联盟
  • 北京网站维护浩森宇特北京网站建设
  • 新科网站建设深圳百度开户
  • 咨询聊城做网站免费一键生成个人网站
  • 网站建设服务合同模板网站关键词seo优化公司
  • 网站建设总结与海外网站cdn加速
  • 网站网站开发的公司电话搜索引擎调词工具哪个好
  • 前后端分离的网站怎么做关键词优化是怎么做的
  • 网站建设与优化推广方案模板站长之家收录查询
  • 网站符号螺蛳粉的软文推广
  • 网站流量刷杭州网站建设技术支持
  • 温州网站建设温州网站制作百度手机网页版入口
  • 客服电话客服系统常德seo快速排名
  • 政府网站群建设 采购需求电脑优化大师
  • php网站后台页面营销策划的八个步骤
  • 电子网站建设基本流程图免费网站入口在哪
  • 商务网站建设定义无经验能做sem专员
  • 2016做网站济南网站seo优化
  • 鲜花网站建设的利息分析网站快速排名服务
  • 网站上的支付接口怎么做永久免费跨境浏览app
  • Ecshop网站建设总结软文推广一般发布在哪些平台
  • 织梦网站安装教程视频教程公司网站怎么优化
  • 达川网站制作淘宝运营培训多少钱
  • 成都网站建设网站推广方式和推广渠道
  • 电商平台运营费用预算肇庆seo按天计费
  • 网站策划书 范文餐饮品牌全案策划
  • 网站系统建设架构河南百度推广公司