Two Sum III – Data structure design (Java)

Design and implement a TwoSum class. It should support the following operations: add and find. add – Add the number to an internal data structure. find – Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); add(3); add(5); find(4) -> true find(7) -> false Java Solution … Read more

LeetCode – Repeated DNA Sequences (Java)

Problem All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. For example, … Read more

LeetCode – Best Time to Buy and Sell Stock IV (Java)

Problem Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you … Read more

Two Sum II – Input array is sorted (Java)

This problem is similar to Two Sum. To solve this problem, we can use two pointers to scan the array from both sides. See Java solution below: public int[] twoSum(int[] numbers, int target) { if (numbers == null || numbers.length == 0) return null;   int i = 0; int j = numbers.length – 1; … Read more

LeetCode – Reverse Bits (Java)

Problem Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up: If this function is called many times, how would you optimize it? Related problem: Reverse Integer Java Solution public int reverseBits(int n) { for (int i … Read more

LeetCode – Pascal’s Triangle (Java)

Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, the result should be: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Java Solution public ArrayList<ArrayList<Integer>> generate(int numRows) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if (numRows <= 0) return result;   ArrayList<Integer> pre = new ArrayList<Integer>(); pre.add(1); result.add(pre);   for (int … Read more

LeetCode – Number of 1 Bits (Java)

Problem Write a function that takes an unsigned integer and returns the number of ’1′ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11′ has binary representation 00000000000000000000000000001011, so the function should return 3. Java Solution public int hammingWeight(int n) { int count = 0; for(int i=1; i<33; i++){ … Read more

LeetCode – Jump Game (Java)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false. Analysis We can … Read more