- 博客(8)
- 资源 (1)
- 收藏
- 关注
原创 [leetcode] House Robber II
和I类似,II变成了环形,即首尾只能取一个。调用两次即可。代码如下: int rob(vector& nums) { if(nums.empty()) return 0; if(nums.size() == 1) return nums[0]; int val = *prev(nums.end());
2015-05-23 18:40:53 322
原创 [leetcode] Add and Search Word - Data structure design
字典树,代码如下: class TrieNode { public: // Initialize your data structure here. TrieNode() { for(int i = 0; i < 26; ++i) next[i] = NULL; end = false; } void ins
2015-05-23 17:46:31 435
原创 [leetcode] Word Search
dfs,代码如下: class Solution { public: bool exist(vector > &board, string word) { const int row = board.size(); const int column = board[0].size(); vector > visited(row, vecto
2015-05-23 17:28:29 364
原创 [leetcode] Word Search II
直接用word search暴力果断超时,根据提示可对words先构建字典树,然后dfs。代码如下: class TrieNode { public: // Initialize your data structure here. TrieNode() { for(int i = 0; i < 26; ++i) next[i] = NULL
2015-05-23 17:27:48 792
原创 [leetcode] Course Schedule II
和Course Schedule类似,记录下结果即可。代码如下: vector findOrder(int numCourses, vector>& prerequisites) { vector result; vector into(numCourses, 0); for(int i = 0; i < prerequisites.size();
2015-05-15 14:41:10 303
原创 [leetcode] Implement Trie (Prefix Tree)
实现字典树基本功能,包括查找key和前缀。代码如下: class TrieNode { public: // Initialize your data structure here. TrieNode() { for(int i = 0; i < 26; ++i) next[i] = NULL; end = false;
2015-05-08 13:59:16 276
原创 [leetcode] Reverse Linked List
链表简单题,遍历头插法即可。代码如下: ListNode* reverseList(ListNode* head) { if(!head) return head; ListNode dummy(-1); dummy.next = head; ListNode *cur = head; Lis
2015-05-07 14:56:48 306
原创 [leetcode] Course Schedule
图算法,判断有向图是否存在回路。拓扑排序即可,代码如下: bool canFinish(int numCourses, vector>& prerequisites) { vector into(numCourses, 0); for(int i = 0; i < prerequisites.size(); ++i) into[prereq
2015-05-07 14:52:21 714
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人