Skip to content

Instantly share code, notes, and snippets.

View baiyanhuang's full-sized avatar

Baiyan Huang baiyanhuang

  • The Trade Desk
  • Shanghai
View GitHub Profile
@baiyanhuang
baiyanhuang / knapsack.cpp
Created June 17, 2012 01:53
#algorithm# 背包问题
// http://www.oiers.cn/pack/Index.html#sec3
// 0-1背包问题:
// 一个小偷带了一个背包去超市偷东西,超市有N件物品,每件物品的重量与价值分别为w[i]和v[i],
// 而小偷的背包最多只能装重量为M的东西 - 小偷该怎么选才能拿走价值最多的东西?
// 这是一个经典的动态规划算法:
// 状态:f(n, m),依次考虑每个物品,f为前n个物品,放入容量为m的背包的最大value,
// 但么不难看出,f(N,M)是我们要求的结果
//
@baiyanhuang
baiyanhuang / mymemcopy.cpp
Created June 17, 2012 01:50
#algorithm# memory copy
#include <stdio.h>
// A homemade memcpy function.
// 1. Handle the memory overlap
// 2. Copy 4 bytes a time first as int, then copy the left ones as char (at most 3)
void* mymemcpy(void* dest, const void* src, size_t count)
{
if(NULL == dest || NULL == src || dest == src)
return dest;
@baiyanhuang
baiyanhuang / queue_by_stack.cpp
Created June 17, 2012 01:49
#algorithm# Implement queue by 2 stacks.
#include <stack>
#include <iostream>
#include <cassert>
#include <LazyTest.h> // my unittest framework
// use 2 stacks to implement a queue - first in first out
// push - pop - front - back - size - empty
// Imagine that 2 stack bend as a "U" shape, with each top as back and front
template <class T>
class queue
@baiyanhuang
baiyanhuang / qsort.cpp
Created June 17, 2012 01:43
quick sort - the inplace swap way
#include <iostream>
#include <functional>
#include <iterator>
#include "../LazyLib/stdext.h"
using namespace std;
int partition1(int* a, int i1, int i2)
{
int x = a[i2];
@baiyanhuang
baiyanhuang / binary_search.cpp
Created June 17, 2012 01:42
#algorithm# binary search using loop
#include <algorithm>
#include <ctime>
#include <vector>
#include <iostream>
using namespace std;
// Binary search for a given value in a vector, and return the index of the first found value (-1 for not found).
// The vector should be sorted in ascending order
// Notice:
// 1. Use unsigned int for index type
@baiyanhuang
baiyanhuang / arraylist.cpp
Created June 17, 2012 01:41
#algorithm# use array to allocate id & implement a linked list
#include <stdio.h>
#include <cassert>
// This class manages available ID use a single linked list, which is implemnent by an array
// We simply use an integer array to save half space as we could resue the "value" feild & the "next" feild:
// 1. The value type is of the same type of index
// 2. The value's range is the same as index
//
const static char LIST_END = -1;
@baiyanhuang
baiyanhuang / disassem.c
Created June 10, 2012 07:15
A dissection of a simple c program's assembly
int main() {
00B61400 push ebp
00B61401 mov ebp,esp
00B61403 sub esp,0C0h // esp指向栈顶
00B61409 push ebx
00B6140A push esi
00B6140B push edi
00B6140C lea edi,[ebp-0C0h]
00B61412 mov ecx,30h //C0h 除以4,就是30h,因为rep stos用的是dword
00B61417 mov eax,0CCCCCCCCh //设置返回值为默认值:无效
@baiyanhuang
baiyanhuang / largetK-heap.cpp
Created June 6, 2012 13:29
get the largest k numbers from an array
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
bool compare(int a, int b)
{
object factorialTest extends App
{
def factorial(n: Int): Int =
{
if(n == 0) 1 else n * factorial(n-1)
}
println(factorial(3))
println(factorial(10))
@baiyanhuang
baiyanhuang / gcd.scala
Created June 5, 2012 13:06
Great common dividor
object sqrtTest extends App
{
def sqrt(x: Double) =
{
def sqrtIter(guess: Double): Double =
{
if(isGoodEnough(guess)) guess
else sqrtIter(improve(guess))
}
def improve(guess: Double) =