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.

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 – 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 – 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

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