1. # LeetCode in Swift: Binary Tree Preorder Traversal

Problem Statement Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3},

return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Original LeetCode problem page My Solution in Swift Continue reading...

2. # LeetCode in Swift: Binary Tree Inorder Traversal

Problem Statement Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3},

return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? Original LeetCode problem page My Solution in Swift Continue reading...

3. # LeetCode in Swift: Binary Tree Postorder Traversal

Problem Statement Given a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3},

return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Original LeetCode problem page My Solution in Swift Continue reading...

4. # LeetCode in Swift: Search Insert Position

Problem Statement Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 2 [1,3,5,6], 2 1 [1,3,5,6], 7 4 [1,3,5,6], 0 0 Original LeetCode problem page My Solution Continue reading...

5. # LeetCode in Swift: Insertion Sort List

Problem Statement Sort a linked list using insertion sort. Original LeetCode problem page My Solution in Swift Every time after inserting a list node, I use a variable to record the reference to that node. When next uninserted list node comes in, it checks from the recorded last inserted node instead of restarting the checking from the head list node. Continue reading...

6. # LeetCode in Swift: Sort List

Problem Statement Sort a linked list in O(n log n) time using constant space complexity. Original LeetCode problem page My Solution in Swift I used a bottom-up iterative merge-sort to solve this problem with O(n log n) time efficiency and O(1) space efficiency. Note that, you cannot use traditional recursive merge-sort to tackle this problem; Continue reading...

7. # LeetCode in Swift: Max Points on a Line

Problem Statement Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Original LeetCode problem page My Solution in Swift The basic idea is to calculate gradient of every pair of points, and group points with same gradient. The final answer is then the size Continue reading...

8. # LeetCode in Swift: Evaluate Reverse Polish Notation

Problem Statement Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples:

Original LeetCode problem page My Solution in Swift Continue reading...

9. # A Beautiful Linear Time Python Regex Matcher via NFA

I came across a problem on leetcode a few days ago. It asked me to implement a simple regex (regular expression) matcher which supports both . and * qualifiers. A brute force algorithm for this simple case alone could end up an exponential time efficiency, not to mention if it were to support all basic regex operators, i.e. Continue reading...

10. # Fix Python graph-tool “couldn’t recognise svg” Problem

After I had installed graph-tool and gtk+3 via homebrew and tried to run a graph-tool demo, I came across the following problem:

I then did a little bit Googling and found out that this was caused by a lack of SVG lib ‘librsvg’ for homebrew’s gtk installation. However, a simple homebrew installation of that lib didn’t fix Continue reading...