## LeetCode – Dungeon Game (Java)

Example: -2 (K) -3 3 -5 -10 1 10 30 -5 (P) Java Solution This problem can be solved by using dynamic programming. We maintain a 2-D table. h[i][j] is the minimum health value before he enters (i,j). h[0][0] is the value of the answer. The left part is filling in numbers to the table. … Read more

## LeetCode – House Robber (Java)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the … Read more

PDF Update History:Â 5/1/2016,Â 3/19/2016,Â 5/1/2015,Â 12/1/2012

## How Developers Sort in Java?

While analyzing source code of a large number of open source Java projects, I found Java developers frequently sort in two ways. One is using the `sort()` method of `Collections` or `Arrays`, and the other is using sorted data structures, such as `TreeMap` and `TreeSet`.

## 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 – Count and Say (Java)

Problem The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, … 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, then one 1” or 1211. Given an integer n, generate the nth … Read more

## LeetCode – Container With Most Water (Java)

Problem Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most … 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 – Symmetric Tree (Java)

Problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Java Solution – … 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