- 浏览: 130566 次
文章分类
- 全部博客 (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 2185Given a string s, return all th ... -
Leetcode - Factor Combination
2015-08-28 09:53 821Numbers can be regarded as prod ... -
Leetcode - Generate Parentheses
2015-08-08 17:01 485[分析] 第一个思路(错误的~):假设递归函数返回 n - ... -
Leetcode - Word Search
2015-08-03 21:03 479Given a 2D board and a word, fi ... -
Leetcode - Subset
2015-08-02 12:06 916[分析] 三种思路 思路1:每层递归新加一个元素,第一层递归, ... -
Leetcode - Subset II
2015-08-02 12:13 922[分析] 延续Subset三种思路,关键是添加去重处理 思路 ... -
Leetcode - Gray Code
2015-08-01 17:26 546原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation Sequence
2015-08-01 17:19 482原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation II
2015-08-01 10:49 569原题链接:https://leetcode.com/probl ... -
Leetcode - Combination
2015-08-01 08:36 458[分析] 从 n 个数中取 k 个数,第一个数有 n 种取法… ... -
Leetcode - Combination Sum III
2015-07-31 22:04 499[分析] 思路就是枚举k个数所有可能的组合并判断是否符合条件。 ... -
Leetcode - Combination Sum II
2015-07-31 21:06 577[分析] 输入数组中的每个元素至多使用一次,相较于Combin ... -
Leetcode - Combination Sum
2015-07-31 20:21 554Given a set of candidate number ... -
Leetcode - Sudoku Solver
2015-07-31 09:14 434[分析] 做Valid Sudoku时表示3*3区块的下标想得 ... -
Leetcode - N Queues II
2015-07-30 20:52 378[分析] 做完N皇后第一题,这个就so easy~ pu ... -
Leetcode - N-Queens
2015-07-30 20:38 407[分析] N皇后摆放规则:两个皇后不能共存于同一行、同一列以及 ... -
Leetcode - Word Ladder II
2015-06-26 09:19 501Given two words (start and end) ... -
Leetcode - Combination Sum III
2015-06-10 10:09 512Find all possible combinati ... -
Leetcode - Palindrome Partition
2015-05-21 09:56 748Given a string s, partition s s ... -
Leetcode - WordBreak III
2015-04-16 08:30 425Given a string s and a dictio ...
相关推荐
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.摆
* [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]...
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一些 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....
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/ 字符串中的 ...
II 解决方法:Majority Voting算法的变种。但是最终的算法实现形式,很难理解。 2018-08-19 19:16 LeetCode: 79. Word Search 解决方法:DFS LeetCode: 31. Next Permutation 解决方法:掌握排列组合的字典序规律,...
leetcode力扣是什么 leetcode-按类别 看了一些leetcode刷题指南,总结一下两个重点,一是最好按照类别刷题,总结思路;二是由易到难,不要产生太大挫败感。...Search II (hard) DFS /二叉树 the difference between df
Search)+ 鸽笼原理(Pigeonhole Principle) “不允许修改数组” 与 “常数空间复杂度”这两个限制条件意味着:禁止排序,并且不能使用Map等数据结构 小于O(n2)的运行时间复杂度可以联想到使用二分将其中的一个n...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。 boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ...
search_word79.cpp。 链表基本操作要进行删除元素,那么在循环的过程中尽量使用前驱进行循环。提前校验head,head->next。new一个空头部。头插法pcur是空头后一位。 对于查找查找类的题目首先考虑二分查找,但是二分...
leetcode/0079-word-search - 我的解决方案太冗长了(逻辑很好) hackerrank/data_structures/trie/contacts.py 我做了一个 trie 的完整实现,但实际上这个问题不需要前缀补全,只需要知道子树中的节点数,这在插入...
word源码java awesome-leetcode 用Java或者python实现的leetcode代码, java相关代码主要来源于 大佬,特此说明,感谢大佬的整理, 我只是为了方便继续写我的python代码,重新更改了许多文件。 Easy # Title Tag 1 ...