- 浏览: 137569 次
文章分类
- 全部博客 (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
iven a 2D board and a list of words from the dictionary, find all words in the board.
Each word must 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 in a word.
For example,
Given words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Return ["eat","oath"].
[分析] 直接的思路是调用Word Search的方法判断输入数组中的各个单词是否在board中,这种方法是低效的。网上ljiabin 同学的博客给出了一个较好的思路,使用输入单词数组构造一个Trie字典,穷搜一遍board判断哪些字符组合出现在字典中则加入到结果集中。该方法两个好处,一是仅需穷搜一遍,二是穷搜过程中可以剪枝计算,一旦当前形成的单词甚至不是Trie树的前缀则无需判断其后续递归。为避免重复,先使用Set获取结果集最后转为List。
自己实现时使用StringBuilder表示当前已形成的单词再各层递归中传递,在{{'a','b'},{'c', 'd'}},"acdb"这个case fail了好久,最后debug出来是因为每次递归后没有恢复到初始状态。递归可以更新结果,但每层递归结束切记要恢复相关状态变量为开始本次递归时的状态,想到了那首经典的诗,“我挥一挥衣袖,不带走一片云彩” O(∩_∩)O~
[ref]
ljiabin 同学的博客
http://blog.csdn.net/ljiabin/article/details/45846527
Each word must 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 in a word.
For example,
Given words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Return ["eat","oath"].
[分析] 直接的思路是调用Word Search的方法判断输入数组中的各个单词是否在board中,这种方法是低效的。网上ljiabin 同学的博客给出了一个较好的思路,使用输入单词数组构造一个Trie字典,穷搜一遍board判断哪些字符组合出现在字典中则加入到结果集中。该方法两个好处,一是仅需穷搜一遍,二是穷搜过程中可以剪枝计算,一旦当前形成的单词甚至不是Trie树的前缀则无需判断其后续递归。为避免重复,先使用Set获取结果集最后转为List。
自己实现时使用StringBuilder表示当前已形成的单词再各层递归中传递,在{{'a','b'},{'c', 'd'}},"acdb"这个case fail了好久,最后debug出来是因为每次递归后没有恢复到初始状态。递归可以更新结果,但每层递归结束切记要恢复相关状态变量为开始本次递归时的状态,想到了那首经典的诗,“我挥一挥衣袖,不带走一片云彩” O(∩_∩)O~
[ref]
ljiabin 同学的博客
http://blog.csdn.net/ljiabin/article/details/45846527
public class Solution { public List<String> findWords(char[][] board, String[] words) { if (board == null || board.length == 0 || board[0].length == 0 || words == null || words.length == 0) return new ArrayList<String>(); Trie dict = new Trie(); for (int i = 0; i < words.length; i++) dict.insert(words[i]); int rows = board.length; int cols = board[0].length; Set<String> resultSet = new HashSet<String>(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { dfs(board, i, j , new StringBuilder(), new boolean[rows][cols], dict, resultSet); } } return new ArrayList<String>(resultSet); } public void dfs(char[][] board, int i, int j, StringBuilder curr, boolean[][] used, Trie dict, Set<String> result) { if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || used[i][j]) return; curr.append(board[i][j]); String currstr = curr.toString(); if (!dict.isPrefix(currstr)) { curr.deleteCharAt(curr.length() - 1); //容易被忽略,恢复递归初始状态 return; } if (dict.search(currstr)) result.add(currstr); used[i][j] = true; dfs(board, i, j + 1, curr, used, dict, result); dfs(board, i, j - 1, curr, used, dict, result); dfs(board, i + 1, j, curr, used, dict, result); dfs(board, i - 1, j, curr, used, dict, result); // 恢复递归初始状态 used[i][j] = false; curr.deleteCharAt(curr.length() - 1); } } class TrieNode { TrieNode[] children; public static final int ALPHABET_NUM = 26; boolean isAWord; public TrieNode() { children = new TrieNode[ALPHABET_NUM]; } } class Trie { public TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode p = root; for (int i = 0; i < word.length(); i++) { int idx = word.charAt(i) - 'a'; if (p.children[idx] == null) { p.children[idx] = new TrieNode(); } p = p.children[idx]; } p.isAWord = true; } public boolean search(String word) { TrieNode p = root; for (int i = 0; i < word.length(); i++) { int idx = word.charAt(i) - 'a'; if (p.children[idx] == null) return false; p = p.children[idx]; } return p.isAWord; } public boolean isPrefix(String word) { TrieNode p = root; for (int i = 0; i < word.length(); i++) { int idx = word.charAt(i) - 'a'; if (p.children[idx] == null) return false; p = p.children[idx]; } return true; } }
发表评论
-
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 527[分析] 第一个思路(错误的~):假设递归函数返回 n - ... -
Leetcode - Word Search
2015-08-03 21:03 516Given a 2D board and a word, fi ... -
Leetcode - Subset
2015-08-02 12:06 949[分析] 三种思路 思路1:每层递归新加一个元素,第一层递归, ... -
Leetcode - Subset II
2015-08-02 12:13 957[分析] 延续Subset三种思路,关键是添加去重处理 思路 ... -
Leetcode - Gray Code
2015-08-01 17:26 579原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation Sequence
2015-08-01 17:19 515原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation II
2015-08-01 10:49 604原题链接: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 616[分析] 输入数组中的每个元素至多使用一次,相较于Combin ... -
Leetcode - Combination Sum
2015-07-31 20:21 587Given a set of candidate number ... -
Leetcode - Sudoku Solver
2015-07-31 09:14 467[分析] 做Valid Sudoku时表示3*3区块的下标想得 ... -
Leetcode - N Queues II
2015-07-30 20:52 407[分析] 做完N皇后第一题,这个就so easy~ pu ... -
Leetcode - N-Queens
2015-07-30 20:38 445[分析] N皇后摆放规则:两个皇后不能共存于同一行、同一列以及 ... -
Leetcode - Word Ladder II
2015-06-26 09:19 532Given 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 耗时查词二 给定一个 2D 板和字典中的单词列表,找到板中的所有单词。 每个单词必须由顺序相邻单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。 同一个字母单元格不能在一个单词中多次使用...
扩展矩阵leetcode ...63.不同路径II uniquePathsWithObstacles 139.单词拆分 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
* [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添加元素使和等于 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一些 LeetCode 题目的进阶解法,追求极致效率。由于 LeetCode 已启用中文版域名 leetcode-... Word-Search-II题目: | 英文站源码:./problem-0212-Word-Search-II/标签:哈希表,双向链表难度:困难 / Hard
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 不会词搜索 给定一个 2D 板和一个单词,查找该单词是否存在于网格中。 单词可以由顺序相邻的单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。 同一个字母单元格不能多次使用。 Example: ...
圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 从不同的方向思考 简单的 大批 1.Two Sum -> 使用哈希表避免遍历列表448.查找数组中消失的所有数字-> 1.建立缓冲区来计算数字| 2.使用数组作为带符号的缓冲区118....
例如,"Word Search"(单词搜索)问题,可以通过DFS遍历二维数组寻找单词路径。 四、动态规划 动态规划是解决复杂问题的利器,如"Longest Increasing Subsequence"(最长递增子序列)问题。Java中动态规划的实现...
4. 题目79:单词搜索 (Word Search) 在一个二维字符网格中查找给定的单词,使用深度优先搜索(DFS)或广度优先搜索(BFS)策略,结合回溯技术来实现。 5. 题目4:寻找两个有序数组的中位数 (Median of Two Sorted ...
leetcode 苹果 LeetCode_No.208_-实现 Trie (前缀树) 题目介绍 Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完...
硬字搜索IIhttps://leetcode.com/problems/word-search-ii/ EasyFind the Difference https://leetcode.com/problems/find-the-difference/ EasyWord 模式https://leetcode.com/problems/word-pattern/ 字符串中的 ...
- Word Search II:这是一个在二维网格中寻找单词的题目,它涉及到深度优先搜索(DFS)和字典树(Trie)的使用,适合考查候选人的数据结构掌握情况和字符串处理能力。 - Two Sum:这个经典的题目要求找出数组中两个...