Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

07 2014 档案

摘要:Just corner case..class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { if(m == n) return head; ListNode *... 阅读全文
posted @ 2014-07-31 08:06 Tonix 阅读(124) 评论(0) 推荐(0)

摘要:I fell in a deadloop, and didn't see the actual recursion pattern.Reference:http://fisherlei.blogspot.com/2013/03/leetcode-unique-binary-search-trees-... 阅读全文
posted @ 2014-07-31 07:19 Tonix 阅读(95) 评论(0) 推荐(0)

摘要:The only difference with version I is: one number can only be used once:class Solution {public: vector > ret; struct Rec { Rec() : sum... 阅读全文
posted @ 2014-07-31 03:40 Tonix 阅读(106) 评论(0) 推荐(0)

摘要:A very interesting numeric problem. It involves a lot tricks.Linear search (by +\-) is not feasible - too slow. So binary search is a good idea. Also ... 阅读全文
posted @ 2014-07-29 12:25 Tonix 阅读(149) 评论(0) 推荐(0)

摘要:Similar with "Longest Consecutive Sequence". Another usage to hashset.Take care of corner cases!class Solution {public: int firstMissingPositive(in... 阅读全文
posted @ 2014-07-28 12:27 Tonix 阅读(173) 评论(0) 推荐(0)

摘要:A BFS usage.class Solution {public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) return NULL; UndirectedGr... 阅读全文
posted @ 2014-07-28 11:49 Tonix 阅读(131) 评论(0) 推荐(0)

摘要:Similar with "Climb stairs" but with more restrictions.// Ref: http://yucoding.blogspot.com/2013/01/leetcode-question-26-decode-ways.htmlclass Solu... 阅读全文
posted @ 2014-07-28 08:25 Tonix 阅读(131) 评论(0) 推荐(0)

摘要:Typical recurrsion\DFS problem. Just take care of memory use.class Solution {public: vector> ret; void go(int currMaxN, int currK, int k, unorde... 阅读全文
posted @ 2014-07-28 06:12 Tonix 阅读(153) 评论(0) 推荐(0)

摘要:Compared with I version, the tree could be incomplete. The only difference is that, we connect current node's child to next non-childrenless' node's f... 阅读全文
posted @ 2014-07-26 15:01 Tonix 阅读(145) 评论(0) 推荐(0)

摘要:Flood-Fill. BFS. But there's a trick. If we fill surrounded region directly, extra bookkeeping cost is needed - because we don't know whether that reg... 阅读全文
posted @ 2014-07-26 14:09 Tonix 阅读(189) 评论(0) 推荐(0)

摘要:Comparison-based sorting takes O(nlgn), so hashset is a good idea. After keeping records of all numbers in the hashset, you start checking each number... 阅读全文
posted @ 2014-07-25 01:14 Tonix 阅读(208) 评论(0) 推荐(0)

摘要:Nothing to hard to think. Just take care of boundary conditions.class Solution {public: string convert(string s, int nRows) { if(s.empty() |... 阅读全文
posted @ 2014-07-23 12:54 Tonix 阅读(165) 评论(0) 推荐(0)

摘要:Two passes: all pointer linking info will be recorded in 1st pass - to hashmap; 2nd pass recover the pointer relationship from hashmap.1A!class Soluti... 阅读全文
posted @ 2014-07-23 12:39 Tonix 阅读(149) 评论(0) 推荐(0)

摘要:Another Double pointer solution. 1A!class Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode *p = NULL; ... 阅读全文
posted @ 2014-07-23 11:41 Tonix 阅读(120) 评论(0) 推荐(0)

摘要:2 solutions: bin-search and Newton iteration.class Solution {public: int _sqrt(long long tgt, long long i0, long long i1) { long long can... 阅读全文
posted @ 2014-07-23 05:41 Tonix 阅读(169) 评论(0) 推荐(0)

摘要:Another textbook problem. We take back of postorder array as the current root, and then we can split the inorder array: 1st half for current right chi... 阅读全文
posted @ 2014-07-23 04:13 Tonix 阅读(142) 评论(0) 推荐(0)

摘要:A collegiate textbook problem. Nothing special, but just take care of your memory use.class Solution {public: TreeNode *_buildTree(int pre[], int &... 阅读全文
posted @ 2014-07-23 03:29 Tonix 阅读(125) 评论(0) 推荐(0)

摘要:Really interesting BST manipulation problem!class Solution {public: TreeNode *findConn(TreeNode *p) { TreeNode *pTmp = p; while (p... 阅读全文
posted @ 2014-07-22 13:04 Tonix 阅读(123) 评论(0) 推荐(0)

摘要:Now I believe thoughts leading to DP is brutal DFS.. DFS is brutal force enumeration, but that's too much and naive. We need pruning. So, DFS + Prunin... 阅读全文
posted @ 2014-07-22 12:12 Tonix 阅读(162) 评论(0) 推荐(0)

摘要:For the one pass solution... first I tried to build white\blue from red, but not working anyway. Then I referred someone' code, to build red\blue from... 阅读全文
posted @ 2014-07-22 11:31 Tonix 阅读(129) 评论(0) 推荐(0)

摘要:The first solution I figured out is O(n^3). I thought it must be O(n^2) to get max points on a line for a given point.. but after checking several art... 阅读全文
posted @ 2014-07-22 06:56 Tonix 阅读(141) 评论(0) 推荐(0)

摘要:DFS and using stack to validate candidates.class Solution {public: bool isValid(const string &s) { stack stk; for (int i = 0; i &... 阅读全文
posted @ 2014-07-21 11:02 Tonix 阅读(139) 评论(0) 推荐(0)

摘要:The trick is, we can work on a reversed vector - learnt from EPI.class Solution {public: vector plusOne(vector &digits) { std::reverse(digit... 阅读全文
posted @ 2014-07-21 10:32 Tonix 阅读(146) 评论(0) 推荐(0)

摘要:Recursive.class Solution { public: bool isSymmetric(TreeNode *pl, TreeNode *pr) { if (!pl && !pr) return true; if ((pl && !pr)... 阅读全文
posted @ 2014-07-21 10:19 Tonix 阅读(143) 评论(0) 推荐(0)

摘要:My first reaction: move all A elements back by n positions, and start everything from A[0] and B[0]. But a smarter idea is to start everything from th... 阅读全文
posted @ 2014-07-21 10:18 Tonix 阅读(129) 评论(0) 推荐(0)

摘要:Another recursion problem.class Solution {public: int getHeight(TreeNode *p) { if (!p) return 0; int hL = 1; if (p->left) h... 阅读全文
posted @ 2014-07-21 07:38 Tonix 阅读(167) 评论(0) 推荐(0)

摘要:Classic recursion\pruning problem. We can use O(n) space: A[i] = j means [i,j] is occupied.class Solution {public: int ret; bool isValid(int *A,... 阅读全文
posted @ 2014-07-21 06:49 Tonix 阅读(121) 评论(0) 推荐(0)

摘要:Simply care about the boundary cases:class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 && !l2) return l1;... 阅读全文
posted @ 2014-07-21 05:53 Tonix 阅读(160) 评论(0) 推荐(0)

摘要:Apparently BFS is the most obvious one.. but it is not that simple - only constant extra space is provided.Then the only strategy to take is recursion... 阅读全文
posted @ 2014-07-21 04:39 Tonix 阅读(268) 评论(0) 推荐(0)

摘要:Very classic problem. You can brush up your DP and Searching skills.DP:class Solution {public: int maxSubArray(int A[], int n) { // dp[i + 1... 阅读全文
posted @ 2014-07-21 04:19 Tonix 阅读(186) 评论(0) 推荐(0)

摘要:Another recursion-style problem.1. Each count of sub-solution with a certain root should contribute to final count2. With a certain root, the num of l... 阅读全文
posted @ 2014-07-18 14:13 Tonix 阅读(113) 评论(0) 推荐(0)

摘要:A simple 1D searching problem. Binary search of course.But.. STL has already done it for you:class Solution {public: int searchInsert(int A[], int ... 阅读全文
posted @ 2014-07-18 13:51 Tonix 阅读(94) 评论(0) 推荐(0)

摘要:Since no order requirement, we can simply swap the target value to the last non-target-value in the array - if the last non-target-value is not behind... 阅读全文
posted @ 2014-07-18 13:35 Tonix 阅读(119) 评论(0) 推荐(0)

摘要:The best way to learn DP from DFS! Nice problem.My intuition is that it can be solved by DFS:class Solution {public: int climbStairs(int n) { ... 阅读全文
posted @ 2014-07-18 12:49 Tonix 阅读(159) 评论(0) 推荐(0)

摘要:Not very hard to figure out the solution: splitting the list into 2 halves; reverse the 2nd half and interleave the two halves.But it takes some time ... 阅读全文
posted @ 2014-07-18 04:10 Tonix 阅读(146) 评论(0) 推荐(0)

摘要:The key of this problem is about details especially boundary conditions.class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ... 阅读全文
posted @ 2014-07-18 01:01 Tonix 阅读(126) 评论(0) 推荐(0)

摘要:This is to test your knowledge on BST and its traversal.Flatting BST into an array using in-order, and check that array. It is that simple:class Solut... 阅读全文
posted @ 2014-07-17 15:44 Tonix 阅读(143) 评论(0) 推荐(0)

摘要:This is the same asSPOJ #453. The best way to understand DP1A code:class Solution {public: int minimumTotal(vector > &triangle) { fo... 阅读全文
posted @ 2014-07-17 06:21 Tonix 阅读(144) 评论(0) 推荐(0)

摘要:A typical DFS usage, no big deal:class Solution {public: int dfs(TreeNode *p, int pval) { if( !p->left && !p->right ) { ... 阅读全文
posted @ 2014-07-17 05:27 Tonix 阅读(150) 评论(0) 推荐(0)

摘要:It is not a big deal at the first sight:https://oj.leetcode.com/problems/single-number/And my AC code is straightforward:#include using namespace std... 阅读全文
posted @ 2014-07-16 15:39 Tonix 阅读(145) 评论(0) 推荐(0)

摘要:First, 写POJ的报告用毛英文啊...这道题思路不算难,主要参考了这里:http://www.cnblogs.com/damacheng/archive/2010/09/24/1833983.html讲得很清楚,但是代码有点复杂,我个人觉得不需要:循环情况可以当作非循环的一个特殊情况,简单处理... 阅读全文
posted @ 2014-07-16 12:04 Tonix 阅读(439) 评论(0) 推荐(0)

摘要:This is really classic searching problem to solve. The key is pruning.(My reference:http://blog.csdn.net/lyy289065406/article/details/6647960)There ar... 阅读全文
posted @ 2014-07-15 06:27 Tonix 阅读(194) 评论(0) 推荐(0)

摘要:Good reference:http://www.cppblog.com/sdfond/archive/2009/07/31/91761.htmlIt is a classic coverage\dp compression problem. Initially I thought of dp[i... 阅读全文
posted @ 2014-07-07 04:23 Tonix 阅读(179) 评论(0) 推荐(0)

摘要:I tried several solutions except the one fromhttp://love-oriented.com/pack/P03.html (wonderful resource to learn DP), but all failed with WA\TLE. Afte... 阅读全文
posted @ 2014-07-05 04:44 Tonix 阅读(183) 评论(0) 推荐(0)

摘要:DP is, enumeration + pruning. If you can think of enumeration pattern of a problem, plus a recurrence relation later, you are there.Main ref:http://bl... 阅读全文
posted @ 2014-07-01 14:02 Tonix 阅读(123) 评论(0) 推荐(0)

点击右上角即可分享
微信分享提示