Pārlūkot izejas kodu

#feat:leetcode p79题题解

yangyi 2 nedēļas atpakaļ
vecāks
revīzija
f2c084eb52
2 mainītis faili ar 91 papildinājumiem un 0 dzēšanām
  1. 45 0
      src/leetcode/p79/Solution.java
  2. 46 0
      src/leetcode/p79/SolutionTest.java

+ 45 - 0
src/leetcode/p79/Solution.java

@@ -0,0 +1,45 @@
+package leetcode.p79;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/8 20:13
+ * @Description: https://leetcode.cn/problems/word-search/description/
+ * 79. 单词搜索
+ */
+public class Solution {
+    public boolean exist(char[][] board, String word) {
+        /**
+         * 回溯
+         * 从某一个点出发,向四个方向搜索,如果搜索到,则继续搜索,如果搜索不到,则回溯
+         */
+        for (int i = 0; i < board.length; i++) {
+            for (int j = 0; j < board[0].length; j++) {
+                if (dfs(i,j,board,0,word))return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean dfs(int i, int j, char[][] board, int index, String word) {
+        if (index >= word.length()) return true;
+        if (board[i][j] == word.charAt(index)) {
+            if(index+1 == word.length())return true;
+            //当前字符合法
+            char c = board[i][j];
+            board[i][j] = '0';
+            //向左
+            if(i-1 >= 0 && dfs(i-1,j,board,index+1,word))return true;
+            //向右
+            if(i+1 < board.length && dfs(i+1,j,board,index+1,word))return true;
+            //向上
+            if(j-1 >= 0 && dfs(i,j-1,board,index+1,word))return true;
+            //向下
+            if(j+1 < board[0].length && dfs(i,j+1,board,index+1,word))return true;
+            //还原现场
+            board[i][j] = c;
+        }
+        return false;
+    }
+}

+ 46 - 0
src/leetcode/p79/SolutionTest.java

@@ -0,0 +1,46 @@
+package leetcode.p79;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/8 20:15
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private char[][] board;
+    private String word;
+    private boolean expected;
+
+    public SolutionTest(char[][] board, String word, boolean expected) {
+        this.board = board;
+        this.word = word;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][]{
+                {new char[][]{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}}, "ABCCED", true},
+                {new char[][]{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}}, "SEE", true},
+                {new char[][]{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}}, "ABCB", false},
+                {new char[][]{{'A'}}, "A", true},
+                {new char[][]{{'a','b'},{'c','d'}}, "acdb", true},
+        });
+    }
+    @Test(timeout = 1000L)
+    public void exist() {
+        boolean result = solution.exist(board, word);
+        assertEquals(expected, result);
+    }
+}