Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / sqrt.java
Created May 9, 2013 19:09
Sqrt(x)
/*
Implement int sqrt(int x).
Compute and return the square root of x.
Algorithm:
1. binary search
2. make sure the right < sqrt(Integer.MAX_VALUE)
*/
public class Solution {
public int sqrt(int x) {
// Start typing your Java solution below
@daifu
daifu / countAndSay.java
Created May 9, 2013 18:36
Count and Say
/*
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
@daifu
daifu / deleteDuplicates.java
Created May 7, 2013 08:27
Remove Duplicates from Sorted List II
/*
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
*/
/**
@daifu
daifu / deleteDuplicates.java
Created May 7, 2013 08:15
Remove Duplicates from Sorted List
/*
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
*/
/**
* Definition for singly-linked list.
@daifu
daifu / setZeroes.java
Created May 6, 2013 03:35
Set Matrix Zeroes
/*
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Algorithm:
1. No need extra space and it has O(n*m) complexity in time
2. Go throught the matrix and if it meets 0, then set its row and col to ZERO
3. Once complete, then go throught the matrix again and change ZERO to 0;
*/
public class Solution {
@daifu
daifu / isInterleave.java
Created May 5, 2013 18:25
Interleaving String
/*
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
@daifu
daifu / climbStairs.java
Created May 5, 2013 03:24
Climbing Stairs
/*
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Algorithm:
1. it is a DP problem and it is the same as the fibonacci series.
*/
public class Solution {
public int climbStairs(int n) {
@daifu
daifu / searchMatrix.java
Last active December 16, 2015 22:29
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.
For example,
Consider the following matrix:
[
@daifu
daifu / addBinary.java
Created May 2, 2013 07:18
Add Binary
/*
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
Algorithm:
Make sure take care of the edge case when the carry > 0 and aLen < 0, bLen < 0.
@daifu
daifu / minWindow.java
Created May 2, 2013 06:41
Minimum Window Substring
/*
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note: