LeetCode – Palindrome Linked List (Java)

Given a singly linked list, determine if it is a palindrome. Java Solution 1 – Creat a new reversed list We can create a new list in reversed order and then compare each node. The time and space are O(n). public boolean isPalindrome(ListNode head) { if(head == null) return true;   ListNode p = head; … Read more

LeetCode – Implement Queue using Stacks (Java)

Implement the following operations of a queue using stacks. push(x) — Push element x to the back of queue. pop() — Removes the element from in front of queue. peek() — Get the front element. empty() — Return whether the queue is empty. Java Solution class MyQueue {   Stack<Integer> temp = new Stack<Integer>(); Stack<Integer> … Read more

LeetCode – Majority Element II (Java)

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Java Solution This problem is similar to Majority Element I. Time = O(n) and Space = O(1). public List<Integer> majorityElement(int[] nums) { List<Integer> result = new … Read more

LeetCode – Kth Smallest Element in a BST (Java)

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. (1 ≤ k ≤ BST’s total elements) Java Solution 1 – Inorder Traversal We can inorder traverse the tree and get the kth smallest element. Time is O(n). public int kthSmallest(TreeNode root, int k) { Stack<TreeNode> stack = … Read more

LeetCode – Summary Ranges (Java)

Given a sorted integer array without duplicates, return the summary of its ranges for consecutive numbers. For example, given [0,1,2,4,5,7], return [“0->2″,”4->5″,”7”]. Analysis When iterating over the array, two values need to be tracked: 1) the first value of a new range and 2) the previous value in the range. Java Solution public List<String> summaryRanges(int[] … Read more

LeetCode – Wildcard Matching (Java)

Implement wildcard pattern matching with support for ‘?’ and ‘*’. Java Solution To understand this solution, you can use s=”aab” and p=”*ab”. public boolean isMatch(String s, String p) { int i = 0; int j = 0; int starIndex = -1; int iIndex = -1;   while (i < s.length()) { if (j < p.length() … Read more

LeetCode – Substring with Concatenation of All Words (Java)

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, given: s=”barfoothefoobarman” & words=[“foo”, “bar”], return [0,9]. Analysis This problem … Read more

LeetCode – Invert Binary Tree (Java)

Java Solution 1 – Recursive public TreeNode invertTree(TreeNode root) { helper(root); return root; }   public void helper(TreeNode n){ if(n==null){ return; }   TreeNode t = n.left; n.left = n.right; n.right = t;   helper(n.left); helper(n.right); }public TreeNode invertTree(TreeNode root) { helper(root); return root; } public void helper(TreeNode n){ if(n==null){ return; } TreeNode t = … Read more

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