Tony's Log

Algorithms, Distributed System, Machine Learning

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

2014年7月23日

摘要: 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 阅读(151) 评论(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 阅读(125) 评论(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 阅读(172) 评论(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 阅读(147) 评论(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 阅读(128) 评论(0) 推荐(0)

2014年7月22日

摘要: Really interesting BST manipulation problem!class Solution {public: TreeNode *findConn(TreeNode *p) { TreeNode *pTmp = p; while (p... 阅读全文
posted @ 2014-07-22 13:04 Tonix 阅读(127) 评论(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 阅读(165) 评论(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 阅读(134) 评论(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 阅读(147) 评论(0) 推荐(0)

2014年7月21日

摘要: 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 阅读(144) 评论(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 阅读(147) 评论(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 阅读(146) 评论(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 阅读(131) 评论(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 阅读(170) 评论(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 阅读(125) 评论(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 阅读(166) 评论(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 阅读(270) 评论(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 阅读(189) 评论(0) 推荐(0)

2014年7月18日

摘要: 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 阅读(116) 评论(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 阅读(95) 评论(0) 推荐(0)