- 浏览: 137540 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
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.
[分析] 思路是选定的元素开始,上下左右分别进行DFS搜索。在单次DFS搜索中,每个元素只能访问一次,因此DFS过程中需要对已访问的元素进行标记,一种方法是直接在board中修改,标记已访问元素为特殊字符,对其递归完成后恢复原值,另一种方法是使用一个m*n的辅助空间用于维护访问状态,后者代码更简洁。特别要注意:Method2 的DFS 方法中两个结束条件顺序不能调换,写成Wrong version那种在leetcode中会显示超时,事实上答案也有可能不对,考虑例子board=[["ab"]],word="ab", 就会wrong answer,如果board 每行内容很长,有很多行,word就是完整一行的内容,则就会导致超时。 递归时如果有多个结束条件,需要仔细辨别下它们的顺序。多谢yb君指点~
[ref]
http://blog.csdn.net/linhuanmars/article/details/24336987
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.
[分析] 思路是选定的元素开始,上下左右分别进行DFS搜索。在单次DFS搜索中,每个元素只能访问一次,因此DFS过程中需要对已访问的元素进行标记,一种方法是直接在board中修改,标记已访问元素为特殊字符,对其递归完成后恢复原值,另一种方法是使用一个m*n的辅助空间用于维护访问状态,后者代码更简洁。特别要注意:Method2 的DFS 方法中两个结束条件顺序不能调换,写成Wrong version那种在leetcode中会显示超时,事实上答案也有可能不对,考虑例子board=[["ab"]],word="ab", 就会wrong answer,如果board 每行内容很长,有很多行,word就是完整一行的内容,则就会导致超时。 递归时如果有多个结束条件,需要仔细辨别下它们的顺序。多谢yb君指点~
[ref]
http://blog.csdn.net/linhuanmars/article/details/24336987
public class Solution { //Method 2: 辅助数组标记是否被访问 public boolean exist(char[][] board, String word) { if (board == null || board.length == 0 || board[0].length == 0 || word == null || word.length() == 0) return false; int rows = board.length; int cols = board[0].length; boolean[][] used = new boolean[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (dfs(board, word, 0, i, j, used)) return true; } } return false; } public boolean dfs(char[][] board, String word, int idx, int i, int j, boolean[][] used) { if (idx == word.length()) return true; if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || used[i][j] || board[i][j] != word.charAt(idx)) return false; used[i][j] = true; boolean result = dfs(board, word, idx + 1, i, j + 1, used) || dfs(board, word, idx + 1, i, j - 1, used) || dfs(board, word, idx + 1, i + 1, j, used) || dfs(board, word, idx + 1, i - 1, j, used); used[i][j] = false; return result; } // Wrong version private boolean dfs(char[][] board, int i, int j, String word, int curr, boolean[][] used) { if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || used[i][j]) return false; if (curr == word.length()) return true; if (board[i][j] == word.charAt(curr)) { used[i][j] = true; boolean ret = dfs(board, i, j + 1, word, curr + 1, used) || dfs(board, i, j - 1, word, curr + 1, used) || dfs(board, i + 1, j, word, curr + 1, used) || dfs(board, i - 1, j, word, curr + 1, used); used[i][j] = false; return ret; } else { return false; } } // Method 1: 修改原数组元素标记是否被访问 public boolean exist1(char[][] board, String word) { if (board == null || board.length == 0 || board[0].length == 0 || word == null || word.length() == 0) return false; int rows = board.length; int cols = board[0].length; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == word.charAt(0) && recur(board, word, i, j, 1)) return true; } } return false; } public boolean recur(char[][] board, String word, int i, int j, int idx) { if (idx == word.length()) return true; char curr = board[i][j]; char next = word.charAt(idx); // up if (i > 0 && board[i - 1][j] == next) { board[i][j] = '#'; if (recur(board, word, i - 1, j, idx + 1)) return true; board[i][j] = curr; } // down if (i < board.length - 1 && board[i + 1][j] == next) { board[i][j] = '#'; if (recur(board, word, i + 1, j, idx + 1)) return true; board[i][j] = curr; } // right if (j < board[0].length - 1 && board[i][j + 1] == next) { board[i][j] = '#'; if (recur(board, word, i, j + 1, idx + 1)) return true; board[i][j] = curr; } // left if (j > 0 && board[i][j - 1] == next) { board[i][j] = '#'; if (recur(board, word, i, j - 1, idx + 1)) return true; board[i][j] = curr; } return false; } }
发表评论
-
Leetcode - Palindrome Permutation II
2015-08-28 21:17 2214Given a string s, return all th ... -
Leetcode - Factor Combination
2015-08-28 09:53 861Numbers can be regarded as prod ... -
Leetcode - Generate Parentheses
2015-08-08 17:01 526[分析] 第一个思路(错误的~):假设递归函数返回 n - ... -
Leetcode - Word Search II
2015-08-03 21:25 972iven a 2D board and a list of w ... -
Leetcode - Subset
2015-08-02 12:06 949[分析] 三种思路 思路1:每层递归新加一个元素,第一层递归, ... -
Leetcode - Subset II
2015-08-02 12:13 956[分析] 延续Subset三种思路,关键是添加去重处理 思路 ... -
Leetcode - Gray Code
2015-08-01 17:26 579原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation Sequence
2015-08-01 17:19 514原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation II
2015-08-01 10:49 603原题链接:https://leetcode.com/probl ... -
Leetcode - Combination
2015-08-01 08:36 494[分析] 从 n 个数中取 k 个数,第一个数有 n 种取法… ... -
Leetcode - Combination Sum III
2015-07-31 22:04 522[分析] 思路就是枚举k个数所有可能的组合并判断是否符合条件。 ... -
Leetcode - Combination Sum II
2015-07-31 21:06 615[分析] 输入数组中的每个元素至多使用一次,相较于Combin ... -
Leetcode - Combination Sum
2015-07-31 20:21 587Given a set of candidate number ... -
Leetcode - Sudoku Solver
2015-07-31 09:14 466[分析] 做Valid Sudoku时表示3*3区块的下标想得 ... -
Leetcode - N Queues II
2015-07-30 20:52 406[分析] 做完N皇后第一题,这个就so easy~ pu ... -
Leetcode - N-Queens
2015-07-30 20:38 445[分析] N皇后摆放规则:两个皇后不能共存于同一行、同一列以及 ... -
Leetcode - Word Ladder II
2015-06-26 09:19 531Given two words (start and end) ... -
Leetcode - Combination Sum III
2015-06-10 10:09 549Find all possible combinati ... -
Leetcode - Palindrome Partition
2015-05-21 09:56 786Given a string s, partition s s ... -
Leetcode - WordBreak III
2015-04-16 08:30 463Given a string s and a dictio ...
相关推荐
- Word Search: 给定一个m×n的二维字符网格board和一个单词(字符串)word,如果word存在于网格中,则返回true;否则,返回false。 - Minimum Window Substring: 给定两个字符串s和t,找出s中包含t所有字母的最小子...
javascript js_leetcode题解之79-word-search.js
python python_leetcode题解之079_Word_Search
c c语言_leetcode题解之0079_word_search.zip
void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配bool search(word) 如果数据结构中存在字符串与 wor
扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 ...wordBreak 279.完全平方数 numSquares 排序 56.合并区间 merge 75.颜色分类 sortColors 179.最大数 largestNumber 324.摆
hihocoder和leetcode leetcode 目录名 功能 Array 数组 Backtracking 回溯 Bit_Manipulation 位操作 Design 数据结构设计 ...单词搜索:word_search.cpp hihocoder String 文章重排:give_my_text_back.cpp
leetcode添加元素使和等于 October 2nd Reviews 79 word Search 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, ...
leetcode 耗时查词二 给定一个 2D 板和字典中的单词列表,找到板中的所有单词。 每个单词必须由顺序相邻单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。 同一个字母单元格不能在一个单词中多次使用...
leetcode 不会词搜索 给定一个 2D 板和一个单词,查找该单词是否存在于网格中。 单词可以由顺序相邻的单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。 同一个字母单元格不能多次使用。 Example: ...
例如,"Word Search"(单词搜索)问题,可以通过DFS遍历二维数组寻找单词路径。 四、动态规划 动态规划是解决复杂问题的利器,如"Longest Increasing Subsequence"(最长递增子序列)问题。Java中动态规划的实现...
leetcode 刷题笔记 记录一些刷题细节,很惭愧只做了一点微小的工作 4.13 162题. Find Peak Element.Binary search,需要比较nums[mid]和nums[mid+1]. 4.12 212题. Word Search II. 用trie tree存word list,然后dfs. ...
圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 从不同的方向思考 简单的 大批 1.Two Sum -> 使用哈希表避免遍历列表448.查找数组中消失的所有数字-> 1.建立缓冲区来计算数字| 2.使用数组作为带符号的缓冲区118....
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
LeetCode一些 LeetCode 题目的进阶解法,追求极致效率。由于 LeetCode 已启用中文版域名 leetcode-... Word-Search-II题目: | 英文站源码:./problem-0212-Word-Search-II/标签:哈希表,双向链表难度:困难 / Hard
4. 题目79:单词搜索 (Word Search) 在一个二维字符网格中查找给定的单词,使用深度优先搜索(DFS)或广度优先搜索(BFS)策略,结合回溯技术来实现。 5. 题目4:寻找两个有序数组的中位数 (Median of Two Sorted ...
leetcode 苹果 LeetCode_No.208_-实现 Trie (前缀树) 题目介绍 Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完...
单词搜索II(Word Search II)**:这是单词搜索问题的升级版,需要在一个二维字符网格中寻找多个给定单词。此题需要用到回溯法和动态规划,同时理解字符串匹配算法如KMP或Rabin-Karp。 4. **301. 删除无效的括号...
Search 解决方法:DFS LeetCode: 31. Next Permutation 解决方法:掌握排列组合的字典序规律,即可。这个规律找不到,我最后还是直接看答案的。 LeetCode: 581. Shortest Unsorted Continuous Subarray 解决方法:无...