## LeetCode – Reverse Vowels of a String (Java)

Write a function that takes a string as input and reverse only the vowels of a string. Java Solution this is a simple problem which can be solved by using two pointers scanning from beginning and end of the array. public String reverseVowels(String s) { ArrayList<Character> vowList = new ArrayList<Character>(); vowList.add(’a’); vowList.add(’e’); vowList.add(’i’); vowList.add(’o’); vowList.add(’u’); … Read more

## LeetCode – Coin Change (Java)

Given a set of coins and a total money amount. Write a method to compute the smallest number of coins to make up the given amount. If the amount cannot be made up by any combination of the given coins, return -1.

For example:
Given [2, 5, 10] and amount=6, the method should return -1.
Given [1, 2, 5] and amount=7, the method should return 2.

## Rotate Array in Java

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. How many different ways do you know to solve this problem? Solution 1 – Intermediate Array In a straightforward way, we can create a new array … Read more

## LeetCode – Increasing Triplet Subsequence (Java)

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Analysis This problem can be formalized as finding a sequence x, y and z, such that x < y < ... Read more

## LeetCode – Maximum Size Subarray Sum Equals k (Java)

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn’t one, return 0 instead. Note: The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. Example 1: Given nums = [1, -1, 5, -2, 3], … Read more

## LeetCode – Sparse Matrix Multiplication (Java)

Given two sparse matrices A and B, return the result of AB. You may assume that A’s column number is equal to B’s row number. 1. Naive Method We can implement Sum(A_ik * B_kj) -> C_ij as a naive solution. public int[][] multiply(int[][] A, int[][] B) { //validity check   int[][] C = new int[A.length][B[0].length]; … Read more

## LeetCode – Line Reflection (Java)

Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflects the given points. Example 1: Given points = [[1,1],[-1,1]], return true. Example 2: Given points = [[1,1],[-1,-1]], return false. Follow up: Could you do better than O(n2)? Java Solution For this problem, we first find the … Read more

## LeetCode – Nested List Weight Sum II (Java)

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list — whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is … Read more

## LeetCode – Largest Divisible Subset (Java)

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. Example 1: nums: [1,2,3] Result: [1,2] (of course, [1,3] will also … Read more

## LeetCode – Linked List Random Node (Java)

Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? Java Solution This problem is … Read more

## LeetCode – Insert Delete GetRandom O(1) – Duplicates allowed (Java)

Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom(): Returns a random element from current collection of elements. The probability of each element being returned is linearly … Read more

## LeetCode – Product of Array Except Self (Java)

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Java Solution 1 public int[] productExceptSelf(int[] nums) { int[] result = … Read more

## LeetCode – Delete Node in a Linked List (Java)

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling … Read more

## Implement a Queue using an Array in Java

There following Java code shows how to implement a queue without using any extra data structures in Java. We can implement a queue by using an array. import java.lang.reflect.Array; import java.util.Arrays;   public class Queue<E> {   E[] arr; int head = -1; int tail = -1; int size;   public Queue(Class<E> c, int size) … Read more

## LeetCode – Lowest Common Ancestor of a Binary Search Tree (Java)

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. Analysis This problem can be solved by using BST property, i.e., left < parent < right for each node. There are 3 cases to handle. Java Solution 1 – Recursive public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, … Read more