Skip to content

Instantly share code, notes, and snippets.

View fever324's full-sized avatar

Hongfei Li fever324

View GitHub Profile
@fever324
fever324 / postOrderTraversal.java
Created July 23, 2015 12:38
Three way of post order traversal
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@fever324
fever324 / combinations.java
Created July 21, 2015 18:36
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> solution = new ArrayList<>();
dfs(n, k, 1, solution, new ArrayList<Integer>());
return solution;
}
private void dfs(int n, int k, int start, List<List<Integer>> l, List<Integer> path) {
if(k == 0) {
l.add(path);
@fever324
fever324 / RectangleArea.java
Created July 21, 2015 17:46
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Rectangle Area Assume that the total area is never beyond the maximum possible value of int.
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int areaA = (D - B) * (C - A);
int areaB = (H - F) * (G - E);
Point a = new Point(A,B);
Point b = new Point(C,D);
Point c = new Point(E,F);
Point d = new Point(G,H);
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@fever324
fever324 / search2DMatrix.java
Last active August 29, 2015 14:22
Search a 2D Matrix
/*
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
*/
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length - 1;
@fever324
fever324 / containsDuplicateII.java
Last active August 29, 2015 14:22
Contains Duplicate II
/*
Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
*/
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
int value = map.get(nums[i]);
if ((i - value) <= k) {
@fever324
fever324 / addTwoNumbers.java
Last active August 29, 2015 14:22
Add two numbers
/*
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
* Definition for singly-linked list.
* public class ListNode {
@fever324
fever324 / ValidateBST.java
Last active August 29, 2015 14:13
Validate BST
/*
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
*
@fever324
fever324 / SameTree.java
Created January 13, 2015 12:03
Same Tree
/*
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
@fever324
fever324 / MergeSortedArray.java
Created January 13, 2015 11:55
Merge Sorted Array
/*
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
*/
public class Solution {
public void merge(int A[], int m, int B[], int n) {