Skip to content

Instantly share code, notes, and snippets.

View hyfrey's full-sized avatar
🏂

hyfrey

🏂
  • Shanghai, Hangzhou
View GitHub Profile
@hyfrey
hyfrey / UniqueBinarySearchTreesII.cpp
Created October 18, 2012 08:40
leetcode Unique Binary Search Trees II
/*
Given n, generate all structurally unique BST's (binary search trees) that
store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
@hyfrey
hyfrey / InterleavingString.cpp
Created October 6, 2012 13:07
leetcode Interleaving String
class Solution {
public:
bool dfs(const string &s1, int i, const string &s2, int j, const string&s3, int k, vector<vector<int> > &dp) {
if(i == s1.size() && j == s2.size() && k == s3.size()) {
return true;
}
if(i == s1.size()) {
return s2.substr(j) == s3.substr(k);
}
@hyfrey
hyfrey / MergeIntervals.cpp
Created October 4, 2012 07:51
leetcode Merge Intervals
/*
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
*/
/**
* Definition for an interval.
@hyfrey
hyfrey / MinimumPathSum.cpp
Created October 4, 2012 06:12
leetcode Minimum Path Sum
/*
Given a m x n grid filled with non-negative numbers,
find a path from top left to bottom right which minimizes
the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
*/
class Solution {
public:
@hyfrey
hyfrey / SymmetricTree.cpp
Created October 1, 2012 12:00
leetcode Symmetric Tree
/*
Symmetric Tree
Given a binary tree, check whether it is a mirror of itself
(ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
@hyfrey
hyfrey / WordSearch.cpp
Created October 1, 2012 05:01
leetcode Word Search
/*
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially
cell, where "adjacent" cells are those horizontally or vertically
neighboring. The same letter cell may not be used more than once.
For example,
Given board =
@hyfrey
hyfrey / SearchInsertPosition.cpp
Created September 29, 2012 13:09
leetcode Search Insert Position
/*
Given a sorted array and a target value, return the index if the target is found.
If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
@hyfrey
hyfrey / ZigZagConversion.cpp
Created September 29, 2012 10:50
leetcode ZigZag Conversion
/*
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like
this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
@hyfrey
hyfrey / UniqueBinarySearchTrees.cpp
Created September 25, 2012 03:54
leetcode Unique Binary Search Trees
/*
Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
@hyfrey
hyfrey / vf.cpp
Created September 24, 2012 06:38
Cpp Virtual Function
#include <iostream>
using namespace std;
/*
######################################################################################################
######################################################################################################
1
*/