Min Stack

Description Difficulty: 2.0/5.0 URL: https://oj.leetcode.com/problems/min-stack/ Solution It looks like a non-trivial question, but the solution is trivial. The problem requires the MinStack to support four operations: pop(), top(), push(), and getMin() in constant time. The normal stack supports the first...

Copy List with Random Pointer

Description Difficulty: 1.5/5.0 URL: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ Solution The most intuitive solution is to use a hash table to record the mapping relationship of the old nodes to the new nodes. After copying the list by setting the right next pointers, we...

Binary Tree Maximum Path Sum

Description Difficulty: 2.5/5.0 URL: https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ Solution The function maxPathSumHelper(pt, max_sum) return the maximum sum path sum of the path in the subtree rooted at pt, which ends/starts at pt. If the sum is less than 0, then it return zero...

Two Sum

Solution So the brute force solution is pretty straightforward: we go through each pair of numbers and check if their sum equals to the target. The time complexity is . The two solutions are less trivial, but with lower time...

Reverse Linked List II

Description Difficulty: 1.5/5.0 url: https://oj.leetcode.com/problems/reverse-linked-list-ii/ Solution class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode new_head(0); new_head.next = head; ListNode *p1 = &new_head; n -= m; while(--m) p1 = p1->next; ListNode *p2 = p1->next, *p3 =...