摘要:Just corner case..class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { if(m == n) return head; ListNode *...
阅读全文
07 2014 档案
摘要: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-...
阅读全文
摘要:The only difference with version I is: one number can only be used once:class Solution {public: vector > ret; struct Rec { Rec() : sum...
阅读全文
摘要: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 ...
阅读全文
摘要:Similar with "Longest Consecutive Sequence". Another usage to hashset.Take care of corner cases!class Solution {public: int firstMissingPositive(in...
阅读全文
摘要:A BFS usage.class Solution {public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) return NULL; UndirectedGr...
阅读全文
摘要:Similar with "Climb stairs" but with more restrictions.// Ref: http://yucoding.blogspot.com/2013/01/leetcode-question-26-decode-ways.htmlclass Solu...
阅读全文
摘要:Typical recurrsion\DFS problem. Just take care of memory use.class Solution {public: vector> ret; void go(int currMaxN, int currK, int k, unorde...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:Nothing to hard to think. Just take care of boundary conditions.class Solution {public: string convert(string s, int nRows) { if(s.empty() |...
阅读全文
摘要: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...
阅读全文
摘要:Another Double pointer solution. 1A!class Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode *p = NULL; ...
阅读全文
摘要:2 solutions: bin-search and Newton iteration.class Solution {public: int _sqrt(long long tgt, long long i0, long long i1) { long long can...
阅读全文
摘要: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...
阅读全文
摘要:A collegiate textbook problem. Nothing special, but just take care of your memory use.class Solution {public: TreeNode *_buildTree(int pre[], int &...
阅读全文
摘要:Really interesting BST manipulation problem!class Solution {public: TreeNode *findConn(TreeNode *p) { TreeNode *pTmp = p; while (p...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:DFS and using stack to validate candidates.class Solution {public: bool isValid(const string &s) { stack stk; for (int i = 0; i &...
阅读全文
摘要:The trick is, we can work on a reversed vector - learnt from EPI.class Solution {public: vector plusOne(vector &digits) { std::reverse(digit...
阅读全文
摘要:Recursive.class Solution { public: bool isSymmetric(TreeNode *pl, TreeNode *pr) { if (!pl && !pr) return true; if ((pl && !pr)...
阅读全文
摘要: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...
阅读全文
摘要:Another recursion problem.class Solution {public: int getHeight(TreeNode *p) { if (!p) return 0; int hL = 1; if (p->left) h...
阅读全文
摘要: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,...
阅读全文
摘要:Simply care about the boundary cases:class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 && !l2) return l1;...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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 ...
阅读全文
摘要: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...
阅读全文
摘要: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) { ...
阅读全文
摘要: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 ...
阅读全文
摘要:The key of this problem is about details especially boundary conditions.class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ...
阅读全文
摘要: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...
阅读全文
摘要:This is the same asSPOJ #453. The best way to understand DP1A code:class Solution {public: int minimumTotal(vector > &triangle) { fo...
阅读全文
摘要:A typical DFS usage, no big deal:class Solution {public: int dfs(TreeNode *p, int pval) { if( !p->left && !p->right ) { ...
阅读全文
摘要: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...
阅读全文
摘要:First, 写POJ的报告用毛英文啊...这道题思路不算难,主要参考了这里:http://www.cnblogs.com/damacheng/archive/2010/09/24/1833983.html讲得很清楚,但是代码有点复杂,我个人觉得不需要:循环情况可以当作非循环的一个特殊情况,简单处理...
阅读全文
摘要:This is really classic searching problem to solve. The key is pruning.(My reference:http://blog.csdn.net/lyy289065406/article/details/6647960)There ar...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文