# [leetcode] Permutation Sequence

Question: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): “123” “132” “213” “231” “312” “321” Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 […]

# [leetcode] Gas Station

Question: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one […]

# [Leetcode] Flatten Binary Tree to Linked List

Problem: Given a binary tree, flatten it to a linked list in-place. [pastacode lang=”java” message=”” highlight=”” provider=”manual”] // use stack to store right public void flattenTree(TreeNode root) { if (root == null) return root; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode p = root; while (!stack.isEmpty()|| p != null) { if (p.right != null) stack.push(p.right); if […]

# [Leetcode] Triangle

Problem: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [     [2],    [3,4],   [6,5,7],  [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 […]