Skip to content

Instantly share code, notes, and snippets.

View b27lu's full-sized avatar

Bing Lu b27lu

  • Toronto, Ontario, Canada
View GitHub Profile
@b27lu
b27lu / DetermineCounterClockwise.py
Created February 2, 2014 02:52
This gist shows how to determine if two line segments intersects.
#define a class to represent the x and y coordinates of points
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
#judges if point A,B,C are counter-clockwise
def ccw(A,B,C):
return (C.y-A.y)*(B.x-A.x) > (B.y-A.y)*(C.x-A.x)
@b27lu
b27lu / Subsets.java
Created February 11, 2014 03:00
Subsets @ LeetCode
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
if(S == null)
return null;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
//empty set is the subset of any set
result.add(new ArrayList<Integer>());
//sort array to statisfy the requirement of non-descending order
@b27lu
b27lu / Subsets2.java
Created February 11, 2014 03:50
Subsets II @ LeetCode
public class Solution {
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
if(num == null)
return null;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
result.add(temp);
@b27lu
b27lu / Permutation.java
Created February 11, 2014 21:02
Permutation @ LeetCode
/*
*Key point: dfs,
* it's similar to question Subsets; however, we need all elements rather than subsets
* so, we have to scan from beginning of array and need a boolean array to store which element has been visited
*/
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
if(num == null)
return null;
@b27lu
b27lu / CombinationSum.java
Created February 12, 2014 16:19
Combination Sum LeetCode
public class Solution {
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
if(candidates == null)
return null;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
if(target <= 0)
return result;
@b27lu
b27lu / Combination.java
Created February 12, 2014 17:38
Combination question of leetcode
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(n <= 0 || k<=0)
return result;
ArrayList<Integer> temp = new ArrayList<Integer>();
dfsWorker(n, k, result, temp, 1);
return result;
}
@b27lu
b27lu / GenerateParentheses.java
Created February 12, 2014 19:00
Generate Parentheses @ LeetCode
public class Solution {
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
if(n <= 0)
return result;
dfsWorker(n, result, sb, 0, 0);
return result;
@b27lu
b27lu / SymmetricTree.java
Created February 13, 2014 16:27
Symmetric Tree
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@b27lu
b27lu / BinaryTreeMaximumPathSum.java
Created February 19, 2014 21:02
Binary Tree Maximum Path Sum at LeetCode
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@b27lu
b27lu / ConstructBinaryTreefromInorderandPostorderTraversal.java
Created February 20, 2014 04:00
Construct Binary Tree from Inorder and Postorder Traversal at LeetCode
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {