LeetCode – Implement Stack using Queues (Java)

Implement the following operations of a stack using queues. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. empty() — Return whether the stack is empty. Note: only standard queue operations are allowed, i.e., poll(), offer(), peek(), size() and isEmpty() in … Read more

LeetCode – Populating Next Right Pointers in Each Node II (Java)

Follow up for problem “Populating Next Right Pointers in Each Node“. What if the given tree could be any binary tree? Would your previous solution still work? Analysis Similar to Populating Next Right Pointers in Each Node, we have 4 pointers at 2 levels of the tree. Java Solution public void connect(TreeLinkNode root) { if(root … Read more

LeetCode – Basic Calculator (Java)

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces.You may assume that the given expression is always valid. Some examples: “1 + 1” = 2, “(1)” = 1, “(1-(4-5))” = 2 … Read more

LeetCode – Rectangle Area (Java)

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner coordinates. Analysis This problem can be converted as a overlap internal problem. On the x-axis, there are (A,C) and (E,G); on the y-axis, there are (F,H) and (B,D). If … Read more

LeetCode – The Skyline Problem (Java)

Analysis This problem is essentially a problem of processing 2*n edges. Each edge has a x-axis value and a height value. The key part is how to use the height heap to process each edge. Java Solution class Edge { int x; int height; boolean isStart;   public Edge(int x, int height, boolean isStart) { … Read more

LeetCode – Word Ladder II (Java)

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: 1) Only one letter can be changed at a time, 2) Each intermediate word must exist in the dictionary. For example, given: start = “hit”, end = “cog”, and dict = [“hot”,”dot”,”dog”,”lot”,”log”], return: [ [“hit”,”hot”,”dot”,”dog”,”cog”], … Read more

LeetCode – Contains Duplicate III (Java)

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. Java Solution 1 – Simple This solution simple. Its time complexity is O(nlog(k)). … Read more

LeetCode – Count Complete Tree Nodes (Java)

Given a complete binary tree, count the number of nodes. The solution to this problem can be as simple as the following: public int countNodes(TreeNode root) { if(root == null){ return 0; }   return 1 + countNodes(root.left) + countNodes(root.right); }public int countNodes(TreeNode root) { if(root == null){ return 0; } return 1 + countNodes(root.left) … Read more

LeetCode – Construct Binary Tree from Preorder and Inorder Traversal (Java)

Given preorder and inorder traversal of a tree, construct the binary tree. Analysis Consider the following example: in-order: 4 2 5 (1) 6 7 3 8 pre-order: (1) 2 4 5 3 7 6 8 From the pre-order array, we know that first element is the root. We can find the root in in-order array. … Read more

LeetCode – Course Schedule II (Java)

This is an extension of Course Schedule. This time a valid sequence of courses is required as output. Analysis If we use the BFS solution of Course Schedule, a valid sequence can easily be recorded. Java Solution public int[] findOrder(int numCourses, int[][] prerequisites) { if(prerequisites == null){ throw new IllegalArgumentException("illegal prerequisites array"); }   int … Read more

LeetCode – Reverse Linked List II (Java)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Analysis Java Solution public ListNode reverseBetween(ListNode head, int m, int n) { if(m==n) return head;   ListNode prev = null;//track (m-1)th node ListNode first = new ListNode(0);//first’s … Read more

LeetCode – Next Permutation (Java)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and … Read more

LeetCode – Restore IP Addresses (Java)

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. Java Solution This is a typical search problem and it can be solved by using DFS. public List<String> restoreIpAddresses(String s) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>(); ArrayList<String> t = new ArrayList<String>(); dfs(result, s, … Read more