国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

算法leetcode|79. 單詞搜索(rust重拳出擊)

這篇具有很好參考價(jià)值的文章主要介紹了算法leetcode|79. 單詞搜索(rust重拳出擊)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。



79. 單詞搜索:

給定一個(gè) m x n 二維字符網(wǎng)格 board 和一個(gè)字符串單詞 word 。如果 word 存在于網(wǎng)格中,返回 true ;否則,返回 false 。

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

樣例 1:

算法leetcode|79. 單詞搜索(rust重拳出擊),LeetCode力扣算法題目,rust,golang,數(shù)據(jù)結(jié)構(gòu),算法,后端,leetcode

輸入:
	
	board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
	
輸出:
	
	true

樣例 2:

算法leetcode|79. 單詞搜索(rust重拳出擊),LeetCode力扣算法題目,rust,golang,數(shù)據(jù)結(jié)構(gòu),算法,后端,leetcode

輸入:
	
	board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
	
輸出:
	
	true

樣例 3:

算法leetcode|79. 單詞搜索(rust重拳出擊),LeetCode力扣算法題目,rust,golang,數(shù)據(jù)結(jié)構(gòu),算法,后端,leetcode

輸入:

	board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
	
輸出:
	
	false

提示:

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • board 和 word 僅由大小寫英文字母組成

進(jìn)階:

  • 你可以使用搜索剪枝的技術(shù)來優(yōu)化解決方案,使其在 board 更大的情況下可以更快解決問題?

分析:

  • 面對(duì)這道算法題目,二當(dāng)家的再次陷入了沉思。
  • 需要嘗試所有的可能,遍歷是不可少的,使用循環(huán)或者遞歸,遞歸是首選,因?yàn)楸容^容易實(shí)現(xiàn),深度優(yōu)先,回溯,遞歸套娃大法好。
  • 需要遍歷每一個(gè)元素,然后開始從每個(gè)位置使用遞歸套娃大法,分別向上,下,左,右四個(gè)方向進(jìn)行遞歸,重復(fù)操作,直到匹配成功,或者匹配失敗就回溯嘗試其他,需要注意的是不走重復(fù)的位置,所以需要一個(gè)結(jié)構(gòu)標(biāo)記已經(jīng)走過的位置,使用和原二維字符網(wǎng)格相同大小的結(jié)構(gòu)比較容易,空間上應(yīng)該還可以優(yōu)化,但是沒有深究,因?yàn)榭臻g上并沒有浪費(fèi),如果做優(yōu)化,肯定是以犧牲效率為代價(jià),所謂時(shí)間換空間,我不要換。

題解:

rust:

impl Solution {
    pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
        fn check(board: &Vec<Vec<char>>, word: &Vec<char>, idx: usize, visited: &mut Vec<Vec<bool>>, r: usize, c: usize) -> bool {
            if r >= visited.len() || c >= visited[0].len() || visited[r][c] || board[r][c] != word[idx] {
                return false;
            }
            if idx == word.len() - 1 {
                // 完全匹配
                return true;
            }
            visited[r][c] = true;
            // 上下左右遞歸套娃大法
            if check(board, word, idx + 1, visited, r - 1, c)
                || check(board, word, idx + 1, visited, r + 1, c)
                || check(board, word, idx + 1, visited, r, c - 1)
                || check(board, word, idx + 1, visited, r, c + 1)
            {
                return true;
            }
            visited[r][c] = false;
            return false;
        }

        let word = word.chars().collect::<Vec<_>>();
        let mut visited = vec![vec![false; board[0].len()]; board.len()];
        for r in 0..board.len() {
            for c in 0..board[0].len() {
                if check(&board, &word, 0, &mut visited, r, c) {
                    return true;
                }
            }
        }
        return false;
    }
}

go:

func exist(board [][]byte, word string) bool {
    visited := make([][]bool, len(board))
	for i := range visited {
		visited[i] = make([]bool, len(board[0]))
	}

	var check func(idx, r, c int) bool
	check = func(idx, r, c int) bool {
		if r < 0 || r >= len(board) || c < 0 || c >= len(board[0]) || visited[r][c] || board[r][c] != word[idx] {
			return false
		}
		if idx == len(word)-1 {
			// 完全匹配
			return true
		}
		visited[r][c] = true
		// 上下左右遞歸套娃大法
		if check(idx+1, r-1, c) || check(idx+1, r+1, c) || check(idx+1, r, c-1) || check(idx+1, r, c+1) {
			return true
		}
		visited[r][c] = false
		return false
	}

	for r, row := range board {
		for c := range row {
			if check(0, r, c) {
				return true
			}
		}
	}

	return false
}

c++:

class Solution {
private:
    bool check(vector<vector<char>>& board, string& word, int idx, vector<vector<bool>>& visited, int r, int c) {
        if (r < 0 || r >= visited.size() || c < 0 || c >= visited[0].size() || visited[r][c] || board[r][c] != word[idx]) {
            return false;
        }
        if (idx == word.size() - 1) {
            // 完全匹配
            return true;
        }
        visited[r][c] = true;
        // 上下左右遞歸套娃大法
        if (check(board, word, idx + 1, visited, r - 1, c)
            || check(board, word, idx + 1, visited, r + 1, c)
            || check(board, word, idx + 1, visited, r, c - 1)
            || check(board, word, idx + 1, visited, r, c + 1)) {
            return true;
        }
        visited[r][c] = false;
        return false;
    }
public:
    bool exist(vector<vector<char>>& board, string word) {
        vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));

        for (int r = 0; r < board.size(); ++r) {
            for (int c = 0; c < board[0].size(); ++c) {
                if (check(board, word, 0, visited, r, c)) {
                    return true;
                }
            }
        }

        return false;
    }
};

python:

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        def check(idx: int, r: int, c: int) -> bool:
            if r < 0 or r >= len(board) or c < 0 or c >= len(board[0]) or visited[r][c] or board[r][c] != word[idx]:
                return False
            if idx == len(word) - 1:
                return True
            visited[r][c] = True
            if check(idx + 1, r - 1, c) or check(idx + 1, r + 1, c) or check(idx + 1, r, c - 1) or check(idx + 1, r, c + 1):
                return True
            visited[r][c] = False
            return False

        visited = [[False] * len(board[0]) for _ in range(len(board))]
        for r in range(len(board)):
            for c in range(len(board[0])):
                if check(0, r, c):
                    return True

        return False


java:

class Solution {
    public boolean exist(char[][] board, String word) {
        boolean[][] visited = new boolean[board.length][board[0].length];

        for (int r = 0; r < board.length; ++r) {
            for (int c = 0; c < board[0].length; ++c) {
                if (check(board, word, 0, visited, r, c)) {
                    return true;
                }
            }
        }

        return false;
    }

    private boolean check(char[][] board, String word, int idx, boolean[][] visited, int r, int c) {
        if (r < 0 || r >= visited.length || c < 0 || c >= visited[0].length || visited[r][c] || board[r][c] != word.charAt(idx)) {
            return false;
        }
        if (idx == word.length() - 1) {
            // 完全匹配
            return true;
        }
        visited[r][c] = true;
        // 上下左右遞歸套娃大法
        if (check(board, word, idx + 1, visited, r - 1, c)
                || check(board, word, idx + 1, visited, r + 1, c)
                || check(board, word, idx + 1, visited, r, c - 1)
                || check(board, word, idx + 1, visited, r, c + 1)) {
            return true;
        }
        visited[r][c] = false;
        return false;
    }
}

非常感謝你閱讀本文~
歡迎【點(diǎn)贊】【收藏】【評(píng)論】三連走一波~
放棄不難,但堅(jiān)持一定很酷~
希望我們大家都能每天進(jìn)步一點(diǎn)點(diǎn)~
本文由 二當(dāng)家的白帽子:https://le-yi.blog.csdn.net/ 博客原創(chuàng)~文章來源地址http://www.zghlxwxcb.cn/news/detail-700023.html


到了這里,關(guān)于算法leetcode|79. 單詞搜索(rust重拳出擊)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 算法leetcode|66. 加一(rust重拳出擊)

    給定一個(gè)由 整數(shù) 組成的 非空 數(shù)組所表示的非負(fù)整數(shù),在該數(shù)的基礎(chǔ)上加一。 最高位數(shù)字存放在數(shù)組的首位, 數(shù)組中每個(gè)元素只存儲(chǔ) 單個(gè) 數(shù)字。 你可以假設(shè)除了整數(shù) 0 之外,這個(gè)整數(shù)不會(huì)以零開頭。 1 = digits.length = 100 0 = digits[i] = 9 面對(duì)這道算法題目,二當(dāng)家的再次陷入了

    2024年02月14日
    瀏覽(23)
  • 算法leetcode|57. 插入?yún)^(qū)間(rust重拳出擊)

    給你一個(gè) 無重疊的 ,按照區(qū)間起始端點(diǎn)排序的區(qū)間列表。 在列表中插入一個(gè)新的區(qū)間,你需要確保列表中的區(qū)間仍然有序且不重疊(如果有必要的話,可以合并區(qū)間)。 0 = intervals.length = 10 4 intervals[i].length == 2 0 = intervals[i][0] = intervals[i][1] = 10 5 intervals 根據(jù) intervals[i][0] 按

    2024年02月09日
    瀏覽(24)
  • 算法leetcode|60. 排列序列(rust重拳出擊)

    給出集合 [1,2,3,...,n] ,其所有元素共有 n! 種排列。 按大小順序列出所有排列情況,并一一標(biāo)記,當(dāng) n = 3 時(shí), 所有排列如下: \\\"123\\\" \\\"132\\\" \\\"213\\\" \\\"231\\\" \\\"312\\\" \\\"321\\\" 給定 n 和 k ,返回第 k 個(gè)排列。 1 = n = 9 1 = k = n! 面對(duì)這道算法題目,二當(dāng)家的再次陷入了沉思。 如果模擬,按順序生成k個(gè)

    2024年02月12日
    瀏覽(19)
  • 算法leetcode|62. 不同路徑(rust重拳出擊)

    算法leetcode|62. 不同路徑(rust重拳出擊)

    一個(gè)機(jī)器人位于一個(gè) m x n 網(wǎng)格的左上角 (起始點(diǎn)在下圖中標(biāo)記為 “Start” )。 機(jī)器人每次只能向下或者向右移動(dòng)一步。機(jī)器人試圖達(dá)到網(wǎng)格的右下角(在下圖中標(biāo)記為 “Finish” )。 問總共有多少條不同的路徑? 1 = m, n = 100 題目數(shù)據(jù)保證答案小于等于 2 * 10 9 面對(duì)這道算法

    2024年02月17日
    瀏覽(23)
  • 算法leetcode|54. 螺旋矩陣(rust重拳出擊)

    算法leetcode|54. 螺旋矩陣(rust重拳出擊)

    給你一個(gè) m 行 n 列的矩陣 matrix ,請(qǐng)按照 順時(shí)針螺旋順序 ,返回矩陣中的所有元素。 m == matrix.length n == matrix[i].length 1 = m, n = 10 -100 = matrix[i][j] = 100 面對(duì)這道算法題目,二當(dāng)家的再次陷入了沉思。 可以每次循環(huán)移動(dòng)一步,判斷移到邊界就變換方向,巧用數(shù)組可以減少邏輯判斷

    2024年02月08日
    瀏覽(20)
  • 算法leetcode|85. 最大矩形(rust重拳出擊)

    算法leetcode|85. 最大矩形(rust重拳出擊)

    給定一個(gè)僅包含 0 和 1 、大小為 rows x cols 的二維二進(jìn)制矩陣,找出只包含 1 的最大矩形,并返回其面積。 rows == matrix.length cols == matrix[0].length 1 = row, cols = 200 matrix[i][j] 為 ‘0’ 或 ‘1’ 面對(duì)這道算法題目,二當(dāng)家的再次陷入了沉思。 要不是剛做過 84. 柱狀圖中最大的矩形 這

    2024年02月08日
    瀏覽(17)
  • 算法leetcode|89. 格雷編碼(rust重拳出擊)

    n 位格雷碼序列 是一個(gè)由 2 n 個(gè)整數(shù)組成的序列,其中: 每個(gè)整數(shù)都在范圍 [0, 2 n - 1] 內(nèi)(含 0 和 2 n - 1) 第一個(gè)整數(shù)是 0 一個(gè)整數(shù)在序列中出現(xiàn) 不超過一次 每對(duì) 相鄰 整數(shù)的二進(jìn)制表示 恰好一位不同 ,且 第一個(gè) 和 最后一個(gè) 整數(shù)的二進(jìn)制表示 恰好一位不同 給你一個(gè)整數(shù)

    2024年02月04日
    瀏覽(29)
  • 算法leetcode|71. 簡(jiǎn)化路徑(rust重拳出擊)

    給你一個(gè)字符串 path ,表示指向某一文件或目錄的 Unix 風(fēng)格 絕對(duì)路徑 (以 \\\'/\\\' 開頭),請(qǐng)你將其轉(zhuǎn)化為更加簡(jiǎn)潔的規(guī)范路徑。 在 Unix 風(fēng)格的文件系統(tǒng)中,一個(gè)點(diǎn)( . )表示當(dāng)前目錄本身;此外,兩個(gè)點(diǎn) ( .. ) 表示將目錄切換到上一級(jí)(指向父目錄);兩者都可以是復(fù)雜相

    2024年02月12日
    瀏覽(19)
  • 算法leetcode|75. 顏色分類(rust重拳出擊)

    給定一個(gè)包含紅色、白色和藍(lán)色、共 n 個(gè)元素的數(shù)組 nums , 原地 對(duì)它們進(jìn)行排序,使得相同顏色的元素相鄰,并按照紅色、白色、藍(lán)色順序排列。 我們使用整數(shù) 0 、 1 和 2 分別表示紅色、白色和藍(lán)色。 必須在不使用庫內(nèi)置的 sort 函數(shù)的情況下解決這個(gè)問題。 n == nums.length

    2024年02月10日
    瀏覽(16)
  • 算法leetcode|65. 有效數(shù)字(rust重拳出擊)

    算法leetcode|65. 有效數(shù)字(rust重拳出擊)

    有效數(shù)字 (按順序)可以分成以下幾個(gè)部分: 一個(gè) 小數(shù) 或者 整數(shù) (可選)一個(gè) \\\'e\\\' 或 \\\'E\\\' ,后面跟著一個(gè) 整數(shù) 小數(shù) (按順序)可以分成以下幾個(gè)部分: (可選)一個(gè)符號(hào)字符( \\\'+\\\' 或 \\\'-\\\' ) 下述格式之一: 至少一位數(shù)字,后面跟著一個(gè)點(diǎn) \\\'.\\\' 至少一位數(shù)字,后面跟著一個(gè)

    2024年02月15日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包