【LeetCode-面試演算法經典-Java實現】【079-Word Search(單詞搜尋)】,leetcode--java

來源:互聯網
上載者:User

【LeetCode-面試演算法經典-Java實現】【079-Word Search(單詞搜尋)】,leetcode--java
【079-Word Search(單詞搜尋)】【LeetCode-面試演算法經典-Java實現】【所有題目目錄索引】原題

  Given a 2D board and a word, find if the word exists in the grid.
  The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
  For example,
  Given board =

[  ["ABCE"],  ["SFCS"],  ["ADEE"]]

  word = "ABCCED", -> returns true,
  word = "SEE", -> returns true,
  word = "ABCB", -> returns false.

題目大意

  給定一個board字元矩陣,可以從任意一個點開始經過上下左右的方式走,每個點只能走一次,如果存在一條路走過的字元等於給定的字串,那麼返回true

解題思路

  以每一個點作為起點,使用回溯法進行搜尋

代碼實現

演算法實作類別

public class Solution {    public boolean exist(char[][] board, String word) {        // 【注意我們假定輸入的參數都是合法】        // 訪問標記矩陣,初始值預設會設定為false        boolean[][] visited = new boolean[board.length][board[0].length];        // 以每一個位置為起點進行搜尋,找到一個路徑就停止        for (int i = 0; i < board.length; i++) {            for (int j = 0; j < board[0].length; j++) {                if (search(board, visited, i, j, word, new int[]{0})) {                    return true;                }            }        }        return false;    }    /**     * @param board   字元矩陣     * @param visited 訪問標記矩陣     * @param row     訪問的行號     * @param col     訪問的列號     * @param word    匹配的字串     * @param idx     匹配的位置,取數組是更新後的值可以被其它引用所見     * @return     */    private boolean search(char[][] board, boolean[][] visited, int row, int col, String word, int[] idx) {        // 如果搜尋的位置等於字串的長度,說明已經找到找到匹配的了        if (idx[0] == word.length()) {            return true;        }        boolean hasPath = false;        // 當前位置合法        if (check(board, visited, row, col, word, idx[0])) {            // 標記位置被訪問過            visited[row][col] = true;            idx[0]++;            // 對上,右,下,左四個方向進行搜尋            hasPath = search(board, visited, row - 1, col, word, idx ) // 上                    || search(board, visited, row, col + 1, word, idx) // 右                    || search(board, visited, row + 1, col, word, idx) // 下                    || search(board, visited, row, col - 1, word, idx); // 左            // 如果沒有找到路徑就回溯            if (!hasPath) {                visited[row][col] = false;                idx[0]--;            }        }        return hasPath;    }    /**     * 判定訪問的位置是否合法     *     * @param board   字元矩陣     * @param visited 訪問標記矩陣     * @param row     訪問的行號     * @param col     訪問的列號     * @param word    匹配的字串     * @param idx     匹配的位置     * @return     */    public boolean check(char[][] board, boolean[][] visited, int row, int col, String word, int idx) {        return row >= 0 && row < board.length // 行號合法                && col >= 0 && col < board[0].length // 列號合法                && !visited[row][col] // 沒有被訪問過                && board[row][col] == word.charAt(idx); // 字元相等    }}
評測結果

  點擊圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中查看完整圖片。

特別說明 歡迎轉載,轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/47248121】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.