随笔分类 - Data structure and algorithm
摘要:Given one array, and calculate its maximum sum from any sequential subarray setCpp code demo as below: 1 int maxSubarraySum(int arr[], int length, int& beg, int& end) 2 { 3 assert(arr!=0); 4 int maxSum = 0; 5 int tmpSum = 0; 6 beg = 0; end = 0; 7 for (int i = 0; i < length; i++) ...
阅读全文
摘要:Given x and n, calculate its power n: 1 int power(int x, int n) 2 { 3 if (n==0) 4 return 1; 5 else (n%2==0) 6 return power(x*x, n/2); 7 else 8 return x*power(x*x, n/2); 9 }10 11
阅读全文
摘要:Given one binary search tree, and convert it to one double-linked listCpp code demo as below: 1 struct BSTreeNode 2 { 3 int m_value; 4 BSTreeNode* m_pLeft; 5 BSTreeNode* m_pRight; 6 }; 7 8 BSTreeNode* convert(BSTreeNode* phead, bool asRight) 9 {10 if (!phead)11 return NULL;1...
阅读全文
摘要:定义栈的数据结构,要求添加一个min的函数,能够得到栈的最下元素。要求函数min, push, pop的时间复杂度都是O(1) 1 class stackmin { 2 int data[N]; 3 int minidx[N]; 4 int top; 5 public: 6 stackmin() 7 : top(-1) 8 { 9 ::memset(minidx, -1, sizeof(int)*N);10 ::memset(data, 0, sizeof(int)*N);11 }12 13 ...
阅读全文
摘要:给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。要求:空间复杂度O(1),时间复杂度为O(n)。 1 bool func(int n) 2 { 3 return (n&1)==0; // n%2 is more expensive 4 } 5 6 void group_oddeven(std::vector<int>& a, bool (*func)(int)) 7 { 8 int i = 0, j = a.size()-1; 9 int buf = 0;10 while (i < j) {11 if (!func(a[i]...
阅读全文
摘要:编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列 1 template <typename T> 2 void displayHexBin(const T& v) 3 { 4 const unsigned char c2h[] = "0123456789ABCDEF"; 5 const unsigned char c2b[] = "01"; 6 7 unsigned char* p = (unsigned char*)&v; 8 char* buf = new char [sizeof
阅读全文
摘要:一个文件中有40亿个整数,每个整数为四个字节,内存为1GB,写出一个算法:求出这个文件里的整数里不包含的一个整数下面的代码片段仅仅是一个样例。4个字节的整数最大可表示为2^32=4294967296, 一个数一个数的读入内存,建立一个bit map,共需要4294967296个bits(也就是0.5G字节的内存,并没有超过1G内存的限制),读入每一个数,置相应的bit为1。 1 int N = 20; // # of number 2 int M = 1000; // number range 3 std::vector<int> a(N); // can be ...
阅读全文
摘要:http://en.wikipedia.org/wiki/Selection_algorithm1. partition algorithm 1 function partition(list, left, right, pivotIndex) 2 pivotValue := list[pivotIndex] 3 swap list[pivotIndex] and list[right] // Move pivot to end 4 storeIndex := left 5 for i from left to right-1 6 ...
阅读全文
摘要:删除与某个字符相邻且相同的字符,如,abcddef,删除相邻的相同字符d后变为: abcdef。要求:输入字符串,输出删除后的结果。参考STL算法: unique/unique_copy 1 void deldupchar(char* s) 2 { 3 char* i, *j; 4 if (s && *s!='\0') { 5 i = s; j = s+1; 6 while (*j!='\0') { 7 if (*i!=*j) 8 *++i = *j; 9 j...
阅读全文
摘要:A permutation can be obtained by selecting an element in the given set and recursively permuting the remaining elements.At each stage of the permutation process, the given set of elements consists of two parts: a subset of values that already have been processed, and a subset that still needs to be
阅读全文
摘要:显示一组数的全排列和组合程序: 1 void print(const std::vector<int>& s) 2 { 3 static int n = 1; 4 printf("%d:", n++); 5 printf("["); 6 for (int i = 0; i < s.size(); i++) 7 printf(" %d ", s[i]); 8 printf("]\n"); 9 }10 11 void permutation(std::vector<int>&
阅读全文
摘要:求一个正整数的平方根的程序实现:采用加法递增的方式来代替乘法与N进行比较,递增是按照等差数列的方式。 1 int square(int n) 2 { 3 int tmp = 0; 4 for (int i = 1; i < n; i++) { 5 tmp += 2*(i-1)+1; 6 if (tmp == n) 7 return i; 8 continue; 9 }10 if (n!=0) {11 printf("no integer sqare found!\n");12...
阅读全文
摘要:原文来自于:http://www.parallellabs.com/2010/10/25/practical-concurrent-queue-algorithm/多线程队列(Concurrent Queue)的使用场合非常多,高性能服务器中的消息队列,并行算法中的Work Stealing等都离不开它。对于一个队列来说有两个最主要的动作:添加(enqueue)和删除(dequeue)节点。在一个(或多个)线程在对一个队列进行enqueue操作的同时可能会有一个(或多个)线程对这个队列进行dequeue操作。因为enqueue和dequeue都是对同一个队列里的节点进行操作,为了保证线程安全,
阅读全文
摘要:假设有一片内存,大小为m*n, m是每个单元的大小,而且>=8,共有n个这样的单元,如何将它们链接成n个节点的链表,要求不再使用任何其它内存空间。这里给出SGI STL内存分配器的一个简单实现:首先定义一个union数据结构:1 union obj {2 union obj* free_list_link;3 char client_data[1];4 };这个union结构体的最大大小为4bytes (在32bits 平台上),8bytes (在64bits平台上)。假设那片内存的地址为chunk,那么我们可以这样做: 1 obj* current_obj, *next_o...
阅读全文

浙公网安备 33010602011771号