成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Word Search

Aceyclee / 2095人閱讀

摘要:當(dāng)遞歸到第次時,被調(diào)用了次。說明整個已經(jīng)被找到,返回?;氐胶瘮?shù),遍歷整個數(shù)組,當(dāng)函數(shù)返回時,才返回否則在循環(huán)結(jié)束之后返回。

Problem

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.

Example

Given board =

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

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

Note

建立helper函數(shù),當(dāng)board[i][j]和word的第一個字符相等,將board[i][j]置為非字母的其它字符,防止這個元素再一次被調(diào)用,然后遞歸調(diào)用helper函數(shù)判斷board[i][j]的上下左右相鄰的字符是否和word的下一個字符相等并將結(jié)果存入res,再把board[i][j]置回原來的字符(可能和word.charAt(0)相同的字符在board[][]中有多種情況,而此解并非正解,故還原數(shù)組在main函數(shù)里繼續(xù)循環(huán)),然后遞歸返回res到上一層helper函數(shù)。
當(dāng)遞歸到第word.length次時,helper被調(diào)用了word.length+1次。說明整個word已經(jīng)被找到,返回true。
回到main函數(shù),遍歷整個board數(shù)組,當(dāng)helper函數(shù)返回true時,才返回true;否則在循環(huán)結(jié)束之后返回false

Solution update 2018-9
class Solution {
    public boolean exist(char[][] board, String word) {
        if (board == null || board.length == 0 || board[0].length == 0) return word == null || word.length() == 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == word.charAt(0) && helper(board, i, j, 0, word)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    private boolean helper(char[][] board, int i, int j, int index, String word) {
        if (index == word.length()) return true;
        if (i >= 0 && i < board.length && 
            j >= 0 && j < board[0].length 
            && board[i][j] == word.charAt(index)) {
            board[i][j] = "#";
            boolean res = helper(board, i+1, j, index+1, word) ||
                helper(board, i-1, j, index+1, word) ||
                helper(board, i, j+1, index+1, word) ||
                helper(board, i, j-1, index+1, word);
            board[i][j] = word.charAt(index);
            return res;
        }
        return false;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/65783.html

相關(guān)文章

  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0
  • [LeetCode/LintCode] Word Ladder

    摘要:使用,利用其按層次操作的性質(zhì),可以得到最優(yōu)解。這樣可以保證這一層被完全遍歷。每次循環(huán)取出的元素存為新的字符串。一旦找到和相同的字符串,就返回轉(zhuǎn)換序列長度操作層數(shù),即。 Problem Given two words (start and end), and a dictionary, find the length of shortest transformation sequence...

    張金寶 評論0 收藏0
  • [LeetCode/LintCode] Sentence Similarity

    Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...

    dreamtecher 評論0 收藏0
  • [LeetCode/LintCode] Number of Islands [DFS]

    摘要:兩個循環(huán)遍歷整個矩陣,出現(xiàn)則將其周圍相鄰的全部標(biāo)記為,用子函數(shù)遞歸標(biāo)記。注意里每次遞歸都要判斷邊界。寫一個的,寫熟練。 Number of Islands Problem Given a boolean/char 2D matrix, find the number of islands. 0 is represented as the sea, 1 is represented as...

    Fourierr 評論0 收藏0
  • [LeetCode/LintCode] Is Subsequence

    Problem Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) ...

    terasum 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<