Skip to content

Instantly share code, notes, and snippets.

@wfwei
wfwei / gist:5012660
Last active December 14, 2015 02:19
Java generic based on erasure, so only Object are available at runtime
class Father {}
class Child extends Father {}
public class Test {
public static void main(String args[]){
Class<Child> cClass = Child.class;
Class<? super Child> fClass = cClass.getSuperclass();
// This won't compile
// Class<Father> father = fClass.newInstance();
//only produces Object:
@wfwei
wfwei / gist:5013647
Last active December 14, 2015 02:29
Basic C Grammar
#include "stdio.h"
#include "string"
//宏定义方法,注意括号
#define MAX(x,y) ((x)>(y) ? (x) :(y))
#define IS_EVEN(n) ((n)%2==0)
char * invalidPointer(){
char a[100]; //栈内变量,函数返回后消亡
@wfwei
wfwei / gist:5023092
Last active December 14, 2015 03:39
How To Write Recursive Programs-----Don't compute anything more than once
long int Fib(int N){
if(N<=1)
return 1;
else{
return Fib(N-1)+Fib(N-2);
}
}
/**
running time of Fib(N):
@wfwei
wfwei / gist:5036528
Last active December 14, 2015 05:39
Prime
#include "stdio.h"
#include "math.h"
#define MAX 100000000
int prime[MAX]; //在堆上,自动初始化为0
void initPrimes(){
int i, j;
prime[0] = prime[1] = -1; // not prime number
@wfwei
wfwei / gist:5070689
Created March 2, 2013 11:59
Callable & Future
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableAndFuture {
/**
* @param args
@wfwei
wfwei / gist:5237380
Last active December 15, 2015 09:19
启动三个线程,id分别是0,1,2,输出012012012
public class ThreeThread {
public static Long count = new Long(0);
public static void main(String args[]) {
for (int i = 0; i < 3; i++) {
Thread t = new Thread() {
@Override
public void run() {
int c = 3;
while (c>0)
@wfwei
wfwei / gist:5237595
Created March 25, 2013 14:44
Fib...
package mine.old.staff;
public class Fib {
static final int MAX = 93;
static long[] f = new long[MAX];
static {
f[0] = 0;
f[2] = f[1] = 1;
}
@wfwei
wfwei / gist:5656636
Created May 27, 2013 11:46
求连续子数组的最大和 题目详情: 一个整型数组,数组里有正数也有负数。 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值,要求时间复杂度为O(n)。 例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,那么最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18, 请完成函数int MaxSum(int* a,int n)。 答题说明: 当全是负数的情况时,返回最大的那个负数
int MaxSum(int* a,int n)
{
int i=0, maxSum=1<<31, sum=0;
while(i<n){
sum += a[i++];
if(sum>maxSum)
maxSum = sum;
if(sum<0)
sum = 0;
}
@wfwei
wfwei / gist:5669286
Last active December 17, 2015 20:39
字符串转换成整数 题目详情: 输入一个表示整数的字符串,把该字符串转换成整数并输出。例如输入字符串"345",则输出整数345。
// more smarter way to solve overflow problem
int StrToInt(const char* str)
{
//int maxAbs=((unsigned int)~0)>>1,maxAbsDiv=maxAbs/10, maxAbsRem=maxAbs%10;
int maxAbs=0x7FFFFFFF,maxAbsDiv=maxAbs/10, maxAbsRem=maxAbs%10;
int val=0, i=0, sign=1;
while(str[i]==' ')
i++;
if(str[i]=='-' || str[i]=='+'){
if(str[i]=='-'){
@wfwei
wfwei / gist:5702713
Created June 4, 2013 00:36
Berkeley DB usage example
package mine.code.snippet.db;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;