Restore Ip Addresses

layout: post title: Restore IP Addresses comments: true category: Algorithms tag: algorithm, leetcode, DFS — One thing worth noting is that "0.1.001.0" is not a legal IP. The time complexity of this algorithm is as we have four partition points...

Gas Station

layout: post title: Gas Station comments: true category: Algorithms tag: algorithm, leetcode, greedy — Description https://oj.leetcode.com/problems/gas-station/ Difficulty: 4.0/5.0 Solution This is not a easy problem. First thing to clarify is that we can only go from (i+1)_{th}$$ station. Then we...

Subsets i and ii

Description https://oj.leetcode.com/problems/add-two-numbers/ Difficulty: 1.0/5.0 Solution class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode new_head(-1); auto * cur = &new_head; int carry = 0; while(l1 && l2){ cur->next = new ListNode(l1->val + l2->val + carry); cur = cur->next;...

Unique Binary Search Trees I And Ii

layout: post title: Unique Binary Search Trees i and ii comments: true category: Algorithms tag: algorithm, leetcode — Description https://oj.leetcode.com/problems/unique-binary-search-trees/ https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ Difficulty: 1.5/5.0, 3.0/5.0 Solution We can solve the first problem by search with memorization. class Solution { public: int...

Partition List

Description https://oj.leetcode.com/problems/partition-list/ Difficutly: 1.5/5.0 Solution class Solution { public: ListNode *partition(ListNode *head, int x) { ListNode *head1 = new ListNode(-1), * head2 = new ListNode(-1); ListNode * pt = head, *p1 = head1, *p2 = head2; while(pt){ if (pt->val <...