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 / WordBreak2.java
Created March 23, 2014 19:54
Word Break II @ leetcode
public class Solution {
ArrayList<String> wordBreak(String s, Set<String> dict) {
int length = s.length();
//create the word ending character's index list for each character
ArrayList<ArrayList<Integer>> record = new ArrayList<ArrayList<Integer>>(length);
for(int i = 0;i<length;i++)
record.add(new ArrayList<Integer>());
//each character can be the ending character of some word
@b27lu
b27lu / WordBreak.java
Created March 23, 2014 19:53
Word Break @ LeetCode
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if(s == null || s.length() == 0 || dict == null)
return true;
int length = s.length();
//dp[i] is true when the substring from i to the end can be partitioned
boolean[] dp = new boolean[length+1];
for(boolean b : dp)
@b27lu
b27lu / FirstMissingPositive.java
Created March 12, 2014 16:31
First Missing Positive at LeetCode
public class Solution {
public int firstMissingPositive(int[] A) {
int n = A.length;
int i = 0;
while(i<n){
if(A[i] > 0 && A[i] <= n && A[i]-1 != i && A[A[i]-1] != A[i]){
int temp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = temp;
@b27lu
b27lu / ReverseWordsinaString.java
Created March 10, 2014 16:35
Reverse Words in a String at LeetCode
public class Solution {
public String reverseWords(String s) {
if(s == null)
return s;
//trim leading and trailing spaces
String trimmed = s.trim();
//split string using space(s) as delimiter
String[] splitted = trimmed.split("\\s+");
@b27lu
b27lu / SortList.java
Created February 25, 2014 17:45
Sort List at LeetCode
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@b27lu
b27lu / CopyListwithRandomPonterHashMap.java
Created February 24, 2014 16:28
copy list with random pointer using HashMap
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
@b27lu
b27lu / CopyListwithRandomPointer.java
Created February 24, 2014 16:27
copy list with random pointer
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
@b27lu
b27lu / InsertionSortList.java
Created February 21, 2014 02:56
Insertion Sort List
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@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 {
@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 {