<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)212:?jiǎn)卧~搜索 II

          共 5144字,需瀏覽 11分鐘

           ·

          2021-03-17 14:00

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 單詞搜索 II,我們先來看題面:
          https://leetcode-cn.com/problems/word-search-ii/

          Given an m x n board of characters and a list of strings words, return all words on the board.


          Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

          題意


          給定一個(gè) m x n 二維字符網(wǎng)格 board 和一個(gè)單詞(字符串)列表 words,找出所有同時(shí)在二維網(wǎng)格和字典中出現(xiàn)的單詞。

          單詞必須按照字母順序,通過 相鄰的單元格 內(nèi)的字母構(gòu)成,其中“相鄰”單元格是那些水平相鄰或垂直相鄰的單元格。同一個(gè)單元格內(nèi)的字母在一個(gè)單詞中不允許被重復(fù)使用。

          示例


          輸入:board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]

          輸出:["eat","oath"]

          提示:

          m == board.length
          n == board[i].length
          1 <= m, n <= 12
          board[i][j] 是一個(gè)小寫英文字母
          1 <= words.length <= 3 * 104
          1 <= words[i].length <= 10
          words[i] 由小寫英文字母組成
          words 中的所有字符串互不相同


          解題

          方法:前綴樹+深度優(yōu)先搜索。


          public class Solution {
              private TrieNode root = new TrieNode();
              private int[] ro = {-1, 1, 0, 0};
              private int[] co = {0, 0, -1, 1};
              private void find(char[][] board, boolean[][] visited, int row, int col, TrieNode node, Set<String> founded) {
                  visited[row][col] = true;
                  TrieNode current = node.nexts[board[row][col]-'a'];
                  if (current.word != null) founded.add(current.word);
                  for(int i=0; i<4; i++) {
                      int nr = row + ro[i];
                      int nc = col + co[i];
                      if (nr < 0 || nr >= board.length || nc < 0 || nc >= board[nr].length || visited[nr][nc]) continue;
                      TrieNode next = current.nexts[board[nr][nc]-'a'];
                      if (next != null) find(board, visited, nr, nc, current, founded);
                  }
                  visited[row][col] = false;
              }
              public List<String> findWords(char[][] board, String[] words) {
                  Set<String> founded = new HashSet<>();
                  for(int i=0; i<words.length; i++) {
                      char[] wa = words[i].toCharArray();
                      TrieNode node = root;
                      for(int j=0; j<wa.length; j++) node = node.append(wa[j]);
                      node.word = words[i];
                  }
                  boolean[][] visited = new boolean[board.length][board[0].length];
                  for(int i=0; i<board.length; i++) {
                      for(int j=0; j<board[i].length; j++) {
                          if (root.nexts[board[i][j]-'a'] != null) find(board, visited, i, j, root, founded);
                      }
                  }
                  List<String> results = new ArrayList<>();
                  results.addAll(founded);
                  return results;
              }
          }
          class TrieNode {
              String word;
              TrieNode[] nexts = new TrieNode[26];
              TrieNode append(char ch) {
                  if (nexts[ch-'a'] != null) return nexts[ch-'a'];
                  nexts[ch-'a'] = new TrieNode();
                  return nexts[ch-'a'];
              }
          }



          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-200題匯總,希望對(duì)你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)201:數(shù)字范圍按位與

          LeetCode刷題實(shí)戰(zhàn)202:快樂數(shù)

          LeetCode刷題實(shí)戰(zhàn)203:移除鏈表元素

          LeetCode刷題實(shí)戰(zhàn)204:計(jì)數(shù)質(zhì)數(shù)

          LeetCode刷題實(shí)戰(zhàn)205:同構(gòu)字符串

          LeetCode刷題實(shí)戰(zhàn)206:反轉(zhuǎn)鏈表

          LeetCode刷題實(shí)戰(zhàn)207:課程表

          LeetCode刷題實(shí)戰(zhàn)208:實(shí)現(xiàn) Trie (前綴樹)

          LeetCode刷題實(shí)戰(zhàn)209:長(zhǎng)度最小的子數(shù)組

          LeetCode刷題實(shí)戰(zhàn)210:課程表 II

          LeetCode刷題實(shí)戰(zhàn)211:添加與搜索單詞


          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久99在线观看 | 欧美高潮性爱中文字幕在线播放 | 日精品在线 | 久久久伊人网 | 人人撸人人射 |