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

青岛网站设计皆挺青岛博采网络wordpress建音乐

青岛网站设计皆挺青岛博采网络,wordpress建音乐,百度公司网站seo方案,网站广告策划https://leetcode.cn/problems/word-search-ii/description/?envTypestudy-plan-v2&envIdtop-interview-150思路: 1. 我们可以先将这个单词网格转化成一颗字典树,然后再拿words进行搜索。 2. 我们可以先将words装换成字典树,然后在在boa…
https://leetcode.cn/problems/word-search-ii/description/?envType=study-plan-v2&envId=top-interview-150

思路:
1. 我们可以先将这个单词网格转化成一颗字典树,然后再拿words进行搜索。
2. 我们可以先将words装换成字典树,然后在在board中选点看能不能走出一条满足的路径来
对于这两个思路其实差不多,但是1的话如果board太大就很容易超时,而且无用的构建会很多(比如有一个5*5的board,理论一条路径长度应该是25,但是如果words中长的单词都只有5的话我们就根本用不到这么长)。
所以权衡来看第二种思路会更好

思路一(确实会超时):

class Trie {Trie[] children;boolean isEnd;public Trie() {children = new Trie[26];isEnd = true; // 每个被构造出的节点都是一个单词的结尾}}public List<String> findWords(char[][] board, String[] words) {boolean[][] signed = new boolean[board.length][board[0].length];Trie root = new Trie();for(int i = 0; i < board.length; i++) {for(int j = 0; j < board[0].length; j++) {signed[i][j] = true;if(root.children[board[i][j] - 'a'] == null) {root.children[board[i][j] - 'a'] = new Trie();}transfer(board, signed, root.children[board[i][j] - 'a'], i, j);signed[i][j] = false;}}List<String> ans = new ArrayList<>();for(String word : words) {if(search(root, word)) {ans.add(word);}}return ans;}public void transfer(char[][] board, boolean[][] signed, Trie root, int x, int y) {for(int i = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if(xx >= 0 && xx < board.length && yy >= 0 && yy < board[0].length && !signed[xx][yy]) {signed[xx][yy] = true;if(root.children[board[xx][yy] - 'a'] == null) root.children[board[xx][yy] - 'a'] = new Trie();transfer(board, signed, root.children[board[xx][yy] - 'a'], xx, yy);signed[xx][yy] = false;}}}

 思路二:

    class Trie {Trie[] children;String word; // 如果当前节点是不是一个单词的结尾,那么word=这个单词,否则word=""public Trie() {children = new Trie[26];word = "";}}public List<String> findWords(char[][] board, String[] words) {// 将words构建成字典树Trie root = new Trie();for(String word : words) {add(root, word);}boolean[][] signed = new boolean[board.length][board[0].length];HashSet<String> ans = new HashSet<>();// 在board中选取点在Tire中走看是否存在路径for(int i = 0; i < board.length; i++) {for(int j = 0; j < board[0].length; j++) {signed[i][j] = true;dfs(root, board, signed, ans, i, j);signed[i][j] = false;}}return new ArrayList<>(ans);}public void add(Trie root, String word) {for(int i = 0; i < word.length(); i++) {if(root.children[word.charAt(i) - 'a'] == null) {root.children[word.charAt(i) - 'a'] = new Trie();}root = root.children[word.charAt(i) - 'a'];}root.word = word;}/*** 深度优先搜索* @param root 当前节点* @param board 单词网格* @param signed 标记数组* @param ans 答案* @param x 当前位置x坐标* @param y 当前位置y坐标*/public void dfs(Trie root, char[][] board, boolean[][] signed, HashSet<String> ans, int x, int y) {if(root == null) return;if(!Objects.equals(root.word, "")) {ans.add(root.word);}root = root.children[board[x][y] - 'a'];for(int i = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if(xx >= 0 && xx < board.length && yy >= 0 && yy < board[0].length && !signed[xx][yy]) {char ch = board[xx][yy];signed[xx][yy] = true;dfs(root, board, signed, ans, xx, yy);signed[xx][yy] = false;}}if (root != null && root.word != "") ans.add(root.word);}public boolean search(Trie root, String word) {for(int i = 0; i < word.length(); i++) {if(root.children[word.charAt(i) - 'a'] == null) {return false;}root = root.children[word.charAt(i) - 'a'];}return "".equals(root.word);}

http://www.dtcms.com/wzjs/832764.html

相关文章:

  • 网站的登录界面怎么做青浦建设机械网站
  • 网站建设目标初步目标个人 网站 备案
  • 免费高清网站在线观看福州学做网站
  • 安徽工程建设官方网站收录之家
  • 免费ppt模板下载应用安徽网络优化方案
  • 郑州高端网站建设团队个人网站可以做音乐下载网
  • 公司网站建设基本流程网站建设与管理复习题
  • 网站制作价格是多少元网站制作 毕业设计
  • apache 多网站免费做淘客cms网站
  • wordpress你访问的网站不存在制作网页时文字可以做超链接吗
  • 注册网站除了域名dede 网站打开自动加html
  • 网站备案网站简介网站开发实践实验教程
  • 网页制作与网站建设教程浙江建设厅网站首页
  • 国美在线网站建设费用爱心代码编程python
  • 网站程序文件常州做网站的
  • 沭阳网站制作电子商务网站开发项目
  • 天津做网站的公wordpress4绑定多个域名
  • 建设企业网站官网登录网站优化公司电话
  • 网站seo检测工具上网导航下载
  • 站长之家 wordpress想自己做网站 有免费的吗
  • 电脑网站自适应怎么做揭阳网站设计公司
  • 网站被加黑链建设银行国管公积金管理中心网站
  • 十大免费跨境网站wordpress采集图片不显示
  • 如何制作淘宝详情页图片百度移动端优化
  • 搭建电商平台网站建材做网销哪个网站好
  • 高端网站改版网站后台有哪些
  • 河间网站建设推广中铁十六门户登录
  • 做it题的网站甘肃手机版建站系统价格
  • 推广型网站如何建站网站如何制作学校的做
  • 网络设计的原则有哪些小程序seo帝搜软件sem880官网