Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / generateParenthesis.java
Created May 22, 2013 04:05
Generate Parentheses
/*
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
Algorithm:
1. Recursive algorithm that can have 2 options: ( or )
2. Make sure that ( has more or equals num of )
@daifu
daifu / longestCommonPrefix.java
Created May 20, 2013 17:22
Longest Common Prefix
/*
Write a function to find the longest common prefix string amongst an array of strings.
Algorithm:
1. Traverse through all the str inside strs and keep track of the longest
common prefix.
2. Make sure the lenght of the string does not exceed the length of str.
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
@daifu
daifu / maxArea.java
Created May 20, 2013 16:47
Container With Most Water
/*
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 water.
Note: You may not slant the container.
Algorithm:
1. The problem is to find out the largest area that it can create with left and right and axis of y=0.
@daifu
daifu / isValid.java
Created May 17, 2013 16:25
Valid Parentheses
/*
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Algorithm:
1. Stack will be the best data structure here
2. Push char to stack if stack is empty or next char does not match the peek of stack
3. Pop from stack if the peek char is matched with next char.
4. Return true if the stack is empty, or false if not empty.
@daifu
daifu / removeNthFromEnd.java
Created May 17, 2013 16:07
Remove Nth Node From End of List
/*
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
@daifu
daifu / letterCombinations.java
Created May 16, 2013 17:00
Letter Combinations of a Phone Number
/*
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
@daifu
daifu / longestPalindrome.java
Last active December 17, 2015 08:08
Longest Palindromic Substring
/*
Given a string S, find the longest palindromic substring in S.
You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
Algorithm:
1. It is a pretty straight forward brute force algorithm, and just move
the center of the parlindrome forward and keep track of the max palindrome
2. There are 2 different situation:
a. the center can be even
b. the center can be odd
@daifu
daifu / isMatch.java
Last active December 17, 2015 05:59
Wildcard Matching
/*
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
@daifu
daifu / isMatch.java
Created May 9, 2013 20:38
Wildcard Matching
/*
Work on process...........
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
@daifu
daifu / sortColors.java
Created May 9, 2013 19:35
Sort Colors
/*
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
Algorithm:
1. There are 3 separators, red, white, and blue