Skip to content

Commit

Permalink
二刷79
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Sep 22, 2024
1 parent 44ab666 commit 5d9402b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
27 changes: 27 additions & 0 deletions docs/0079-word-search.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,37 @@ Given word = "*SEE*", return *true*.
Given word = "*ABCB*", return *false*.
----

== 思路分析

image:images/0079-01.png[{image_attr}]

image:images/0079-02.png[{image_attr}]

[[src-0079]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0079_WordSearch.java[tag=answer]
----
--
二刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0079_WordSearch_2.java[tag=answer]
----
--
====

== 参考资料

. https://leetcode.cn/problems/word-search/solutions/2361646/79-dan-ci-sou-suo-hui-su-qing-xi-tu-jie-5yui2/?envType=study-plan-v2&envId=selected-coding-interview[79. 单词搜索 - 回溯,清晰图解^]



Binary file added docs/images/0079-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0079-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,18 @@
|{counter:codes}
|{leetcode_base_url}/rotate-string/[796. Rotate String^]
|{doc_base_url}/0796-rotate-string.adoc[题解]
|✅
|✅ 简单用 `contains`;复杂用 KMP 算法。

|{counter:codes}
|{leetcode_base_url}/course-schedule/[207. Course Schedule^]
|{doc_base_url}/0207-course-schedule.adoc[题解]
|✅ 拓扑排序

|{counter:codes}
|{leetcode_base_url}/word-search/[79. Word Search^]
|{doc_base_url}/0079-word-search.adoc[题解]
|✅ 回溯

|===

截止目前,本轮练习一共完成 {codes} 道题。
7 changes: 5 additions & 2 deletions src/main/java/com/diguage/algo/leetcode/_0079_WordSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
* @since 2020-01-03 17:31
*/
public class _0079_WordSearch {
// tag::answer[]
// tag::answer[]

/**
* Runtime: 4 ms, faster than 89.90% of Java online submissions for Word Search.
*
* Memory Usage: 38.4 MB, less than 97.96% of Java online submissions for Word Search.
*
* Copy from: https://leetcode.com/problems/word-search/discuss/27658/Accepted-very-short-Java-solution.-No-additional-space.[Accepted very short Java solution. No additional space. - LeetCode Discuss]
*
* @author D瓜哥 · https://www.diguage.com
* @since 2020-01-03 17:31
*/
public boolean exist(char[][] board, String word) {
if (Objects.isNull(board) || board.length == 0) {
Expand Down Expand Up @@ -76,7 +79,7 @@ private boolean exist(char[][] board, int y, int x, char[] word, int i) {
return existable;
}

// end::answer[]
// end::answer[]


public static void main(String[] args) {
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/diguage/algo/leetcode/_0079_WordSearch_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.diguage.algo.leetcode;

public class _0079_WordSearch_2 {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-21 21:39:49
*/
public boolean exist(char[][] board, String word) {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == word.charAt(0)) {
if (backtrack(board, r, c, word, 0)) {
return true;
}
}
}
}
return false;
}

private boolean backtrack(char[][] board, int row, int column,
String word, int idx) {
if (idx >= word.length()) {
return true;
}
if (row < 0 || row >= board.length
|| column < 0 || column >= board[row].length
|| board[row][column] == ' '
|| board[row][column] != word.charAt(idx)) {
return false;
}
board[row][column] = ' ';
boolean result = backtrack(board, row - 1, column, word, idx + 1)
|| backtrack(board, row + 1, column, word, idx + 1)
|| backtrack(board, row, column - 1, word, idx + 1)
|| backtrack(board, row, column + 1, word, idx + 1);
board[row][column] = word.charAt(idx);
return result;
}
// end::answer[]

public static void main(String[] args) {
_0079_WordSearch_2 solution = new _0079_WordSearch_2();
char[][] board = {
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}};
String w1 = "ABCCED";
boolean r1 = solution.exist(board, w1);
System.out.println((r1) + " : " + r1);

String w2 = "SEE";
boolean r2 = solution.exist(board, w2);
System.out.println((r2) + " : " + r2);

String w3 = "ABCB";
boolean r3 = solution.exist(board, w3);
System.out.println((!r3) + " : " + r3);
}
}

0 comments on commit 5d9402b

Please sign in to comment.