Jump Game

This is No.55 in leetcode. The problem description is: "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." Here are 4 approaches to the …

Combination Sum

This is a problem in leetcode. To solve this problem, I decide to use recursion on it: class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> rec = new ArrayList<Integer>(); search( candidates, target, res, rec, 0); return res; } private void search(int[] arr, int tar, List<List<Integer>>res, List<Integer>rec, int …

Number of 1 Bits

This is No.191 in LeetCode. This is the best solution I think, easy to understand and has a straight logic: public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int num_one = 0; //001000000010001 while( n > 0 ){ // 00010011 & 00000001 num_one += …

Rotate Array

This is No.189 problem in LeetCode. The problem description is: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to …

Dynamic Programming

What is Dynamic Programming? Let’s have a look at the definition on Introduction to Algorithm: "Dynamic programming like the divide-and-conquer method, solves problems by combining the solutions to sub-problems. Different from divide-and-conquer, dynamic programming applies when the sub-problems overlap — that is, when sub-problems share sub-sub-problems. A dynamic programming algorithm solves each sub-sub-problems just once …