随笔分类 - CC++ Programming
摘要: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...
阅读全文
摘要:类模板的模板友元函数定义有2种方式:1. 将友元模板函数直接定义在类模板中。这种方式比较简单直接。2. 将友元模板函数声明在类模板中,定义在类模板之外。这种方式的写法,如果不小心,通常会出现编译没问题,链接时无法解析的错误。以下是一个简单的正确的例子: 1 #include <iostream> 2 #include <vector> 3 4 template <typename T> 5 class Number; 6 7 template <typename T> 8 void print(const Number<T>&
阅读全文
摘要:定义栈的数据结构,要求添加一个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 ...
阅读全文
摘要:(转)C++中extern “C”含义深层探索1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同。作为一种欲与C兼容的语言,C++保留了一部分过程 式语言的特点(被世人称为“不彻底地面向对象”),因而它可以定义不属于任何类的全局变量和函数。但是,C++毕竟是一种面向对象的程序设计语言,为了支 持函数的重载,C++对全局函数的处理方式与C有明显的不同。 2.从标准头文件说起 某企业曾经给出如下的一道面试题: 面试题 为什么标准头文件都有类似以下的结构?#ifndef __INCvxWorks...
阅读全文
摘要: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...
阅读全文
摘要:显示一组数的全排列和组合程序: 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>&
阅读全文

浙公网安备 33010602011771号