Skip to content

Instantly share code, notes, and snippets.

vector<int> inorderTraversal_moris(TreeNode *root) {
vector<int> ans;
TreeNode* curr = root;
while(curr) {
if(curr->left) {
TreeNode* p = curr->left;
//find the right-most child, remember to check cycle
while(p->right && p->right != curr) {
p = p->right;
}
@chuanying
chuanying / Unique Binary Search Trees.cc
Last active December 18, 2015 11:19
1. Next Permutation 2. Unique Binary Search Trees 3. Recover Binary Search Tree 4. Subsets 题目描述参见LeetCode http://leetcode.com/onlinejudge
int numTrees(int n) {
vector<int> dp(n+1, 0);
dp[0] = dp[1] = 1;
for(int i=2;i<=n;i++) {
//there will be one value at the root, with whatever remains
// on the left and right each forming their own subtrees.
// Iterate through all the values that could be the root...
for(int j=0;j<i;j++) {
dp[i] += dp[j] * dp[i-j-1];
}
@chuanying
chuanying / date_diff.cc
Last active December 18, 2015 20:38
date_diff 题目:设计一个算法,把一个含有N个元素的数组循环右移K位,要求时间复杂度为O(N),且空间复杂度为O(1)。
//给定两个日期, 求他们相差的天数
bool leapyear(int y) {
return (y%4 == 0) && (0 != y%100 || y%400 == 0);
}
int days(int y, int m, int d) {
//static int mt[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int mt[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int x = y-1;
int ans = x*365 + x/4 - x/100 + x/400;
@chuanying
chuanying / Lawnmower.cc
Last active December 18, 2015 23:09
#手写代码竞赛# 6月28日前需要完成的题目: Combination Sum 2, Restore IP Addresses, Best Time to Buy and Sell Stock III, Insert Interval, Lawnmower 前四题目描述见LeetCode http://leetcode.com/onlinejudge 最后一题是Google Code Jam今年资格赛的第二题,也有Online Judge https://code.google.com/codejam/contest/2270488/dashboard#s=p1
//https://code.google.com/codejam/contest/2270488/dashboard#s=p1
int main()
{
int test_case_num = 0;
cin >> test_case_num;
for (int test_case=1; test_case <= test_case_num; test_case++) {
int N, M;
cin >> N >> M;
vector<vector<int> > lawn(N, vector<int>(M, 100));
for (int i = 0; i < N; i++) {
@chuanying
chuanying / Distinct Subsequences.cc
Created July 5, 2013 13:39
# 手写代码竞赛 7月6日前要完成的题目 1. Maximal Rectangle 1. Interleaving String 1. Distinct Subsequences 1. Implement strStr 1. Search for a Range 题目描述见 Leetcode <http://leetcode.com/onlinejudge>
//f(i,j) = f(i+1, j) + (S[i] == T[j]) * f(i+1,j+1)
//means numDistinct(S.substr(i), T.substr(j))
int numDistinct(string S, string T) {
vector<vector<int> > dp(S.length() + 1, vector<int>(T.length() + 1, 0));
dp[S.length()][T.length()] = 1;
for (int i = S.length() - 1; i >= 0; --i) {
dp[i][T.length()] = 1; //!! empty is every string's subseq
for (int j = T.length() - 1; j >= 0; --j) {
dp[i][j] = dp[i+1][j];
if (S[i] == T[j]) {
@chuanying
chuanying / System Design Problems.md
Last active December 20, 2015 20:38
1. 英文很着急, 大家将就着看. 1. 所有题目都是在线的(即对响应时间有要求, 40ms是一个较好的临界值) 1. 所有题目都有Follow up, 那就是根据数据量选择机型, 并估计机器数量 1. 开放式题目, 没有标准答案...

System Design Problems:

  1. Amazon S3

    1. Support one file more than 1TB, how to design the system to meet the requirement?
  2. TinyURL:

    1. We should provide a tinyurl service(two main interface: post long url to get the tinyurl, post tinyurl to get the tinyurl). The tinyurl should be as shorter as possible.
      1. More than 10 billion url in total
      2. More than 100 thousands long url to tinyurl requests per second
  3. More than 1 million tinyurl to long url requests per second

@chuanying
chuanying / problems_20130817.md
Created August 18, 2013 03:40
20130817 线下聚会总结
@chuanying
chuanying / 2011_theta_gas_station.cc
Created August 31, 2013 08:43
codility.com 2011Theta_gas_station
// you can also use includes, for example:
// #include <algorithm>
#include <list>
int solution(const vector<int> &D, const vector<int> &P, int T) {
// write your code here...
list< pair<int, int> > tank; //油箱里面都装了些什么油,由便宜到贵排序的
int curr = 0;
long long cost = 0;
for (int i = 0; i < D.size(); ++i) {
while (!tank.empty() && P[i] < tank.back().first) { //油箱里面有比这个加油站还贵的油, 全部扔掉
@chuanying
chuanying / string split.cc
Created September 10, 2013 05:30
string split.cc 序列化多个字符串, 网络传到另一台机器, 再反序列化他
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <math.h>
@chuanying
chuanying / WordLadderII.cpp
Created September 24, 2013 12:35
Word Ladder II
class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string> > result;
unordered_set<string> visited;
unordered_map<string, vector<string> > traces;
unordered_set<string> curr;
curr.insert(start);
dict.insert(end);
visited.insert(start);