![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C/C++
文章平均质量分 62
kingbird_Wang
努力奋斗中...
展开
-
排序算法 - 计数排序(C++)
/*************************************************** * array: * array that to be sorted. * nLength: * length of array * nK: * 可能出现的最大数字(对10进制数排序则nK=9).******************************************原创 2012-04-07 15:25:12 · 934 阅读 · 0 评论 -
Anagrams 变位词
本文要讨论的是变位词,也就是说通过交换一个单词的各个字母的顺序能变成另一个单词,那么这两个单词互为变位词。问题一:判断给定的两个单词(标准ASCII)是否变位词方法一:用两个数组分别统计两个字符串里每个字符出现的个数,如果完全一致,则是变位词,否则不是.这个方法是大小写敏感的。这个方法能支持的输入字符串的最大长度 将受 用来统计字符个数的数组的类型 的限制。如果用unsigned ch原创 2013-01-09 15:40:52 · 1298 阅读 · 0 评论 -
二维数组中的查找 Cracking the coding interview 9.6
Cracking the coding interview 9.6Given a matrix in which each row and each column is sorted. write a method to find an element in it. 实现一个函数,在一个行列都排序的二维数组中查找某个元素是否存在。思路:如果待搜索元素小于一行中最左边的元素或者大于一行中最原创 2013-01-10 14:49:07 · 871 阅读 · 0 评论 -
排序算法 - 堆排序 (C++)
堆排序的基本过程:1. 找到最大的元素,放到最后面去2. 找到第二大的元素,放在倒数第二个位置。。。依次类推,直到所有元素完成排序。其中每一步中,找最大元素的方法是借助堆。堆可以看成是一个完全二叉树,且每个节点的值都大于它的子节点的值。对于数组来说,就是任意第[i]个元素大于第[2i+1]和第[2i+2]个元素。为了将无序数组变成堆,需要从叶到根依次建堆。也就是从 从后往前第一个有原创 2012-04-06 18:04:46 · 607 阅读 · 0 评论 -
判断一个二叉树是否是平衡二叉树 Cracking the coding interview 4.1
平衡二叉树的定义是:任意节点的左子树的高度和右子树的高度之差小于等于1.那么一个二叉树是平衡二叉树 当且仅当 (1,左子树是平衡二叉树, 2. 右子树是平衡二叉树; 3, 左右子树的高度之差小于等于1).所以用递归的方法判断的话,递归函数就需要返回两个信息:是否平衡,树高度. 代码如下。bool IsBalance(Node *pRoot, int & nDeepth){ if原创 2013-01-15 17:25:06 · 1882 阅读 · 0 评论 -
输入一个整数数组,返回所有元素两两之差绝对值最小的值,O(n)算法
输入一个整数数组,返回所有元素两两之差绝对值最小的值(只要得出最 小值即可,不需要求出是哪两个数)。两两之差的绝对值的最小值,也就是在找所有元素中离得最近的两个元素的距离。我有一个O(n)的算法,用空间换来的。方法如下:申请一个长度为UINT_MAX的bool型数组isExist[],初始值均为false。第一阶段,遍历输入数组,并记录数组的最大值最小值对于ai,将isEx原创 2013-01-30 10:18:35 · 4040 阅读 · 3 评论 -
排列组合问题汇总
问题一 打印出一个集合中的元素的所有可能的组合.思路如下:元素个数为n,可以用一个n位二进制数的每一个bit对应集合中的每一个元素。任取一个n位二进制数即能够表示对该集合中元素的一种选择方案。规则如下:某bit为1,表示选中该元素,为0表示不选中该元素。则从1,累加到(2^n) - 1,即可得到所有组合方案。程序如下:void combine(const char* str, cons原创 2013-01-18 17:17:25 · 1108 阅读 · 0 评论 -
Excel sheet的列名到列序号的转换
序列 seq=[a,b,…,z,aa,ab,…,az,ba,bb,…,bz,…,za,zb,…,zz,aaa,…]类似于excel的列名从小到大依次排列。任意给一字符串 s=[a-z]+(由a-z字符串组成的任意长度字符串),返回s是序列seq的第几个(0-based)字符串。解法1: 先计算1-based下标,返回时减1。注意,这个方法没考虑越界.unsigned int ColumnT原创 2012-12-14 14:22:36 · 3397 阅读 · 0 评论 -
Refactor: replace type code with class (c++)
重构前class Person {public: static const int A = 0; static const int B = 1; static const int AB = 2; static const int O = 3; Person(int bloodType) { this->bloodType = bloodType; } int getB原创 2016-07-15 15:46:30 · 473 阅读 · 0 评论 -
windows API 实现精确的打点计时器
利用windows API QueryPerformanceFrequency()和QueryPerformanceCounter()写了一个计算精确时间差的类.用法:initStart()的时候开始计时,此后每调一次GetTickDelta()返回的是距离上次调用GetTickDelta()所经过的毫秒数。class MyTick{public: MyTick() { LARG原创 2016-09-22 22:37:05 · 639 阅读 · 0 评论 -
利用Windows API实现精确计时器
class CPreciseTimer{public: CPreciseTimer(unsigned int, ITimerObserver*); void Start(); void Pause(); void Terminate(); ~CPreciseTimer(void);private: TimerThreadParam m_param; CWinThread *m原创 2016-09-22 22:42:18 · 2659 阅读 · 0 评论 -
两个已排序数组的归并
Cracking the coding interview 9.1You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.思路:从后向前归并。这里有个问题需要原创 2013-01-06 16:37:22 · 1229 阅读 · 0 评论 -
输出n对括号的所有有效(左右括号成对匹配)排列
原题:Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.EXAMPLE:input: 3 (e.g., 3 pairs of parentheses)output: ()()(), ()(()), (())原创 2012-12-26 16:38:58 · 2340 阅读 · 0 评论 -
排序算法 - 基数排序(C++)
/* * Get the spcific digit of given number. * For example, number 234, * the 0st digit is 4, * the 1st digit is 3, * the 2nd digit is 2, * the 3th digit is 0. */int GetNDigit(int nNumber, i原创 2012-04-07 15:36:25 · 6577 阅读 · 1 评论 -
排序算法 - 快速排序(C++)
// 单向扫描就地重排int Partitation_1Way(int array[], int nIdxLeft, int nIdxRight){ assert(NULL != array); if (nIdxLeft == nIdxRight) return nIdxLeft; // Take the last element as middle value. int nM原创 2012-04-07 11:21:59 · 528 阅读 · 0 评论 -
递归方法反转单向链表(C/C++)
struct Node* RevertLink_Recursive(struct Node* pHead){ if (NULL == pHead) return NULL; if (NULL == pHead->pNext) return pHead; struct Node* pCur = pHead->pNext; // 暂存pHead->pNext struct Node*原创 2012-02-20 13:33:34 · 1084 阅读 · 0 评论 -
从预编译结果中得到某个符号被include的路径
当头文件很多,引用关系比较复杂的时候,怎样知道编译过程中,编译器是通过怎样的路径找到某个符号(变量,函数,类)的声明的. 这可以通过查看预编译的输出结果来得到。举例说明.三个头文件,一个cpp文件.Simple1.h#ifndef Project1_Simple1_h#define Project1_Simple1_hclass Circle{public: Circle(in原创 2012-04-07 14:29:52 · 660 阅读 · 0 评论 -
clang编译器, __declspec(novtable)修饰的class 如果虚函数不声明为纯虚则链接失败.
遇到一个和novtable有关的编译链接问题。class __declspec(novtable) Base{public: // clang 3.1 cannot build without following "=0". But VC can build. virtual bool function(int i)/*=0*/;}; class Child : pu原创 2012-03-22 15:03:43 · 965 阅读 · 0 评论 -
自定义TRACE函数,用于向Output Window、控制台和文件输出Log
自己写了一个Multi-Byte和UNICODE两个版本的TRACE函数。#include #include #define BUFSIZE_MAX 1024#ifdef _DEBUG#ifdef UNICODEstatic void MYTRACE(wchar_t *fmt, ...){ // 取得时间戳 [年-月-日 时:分:秒] SYSTEMTIME time; Ge原创 2012-05-05 07:24:26 · 1570 阅读 · 0 评论 -
利用数组实现双端队列(C++)
/* * 用数组实现一个两端都能插入和删除操作的双端队列 */static const int nLength = 5;static int deque[nLength];static int nIdxHead = 0;static int nIdxTail = 0;enum Err{ NoErr, errOverFlow, errUnderFlow,};// 头进原创 2012-04-07 19:58:00 · 3815 阅读 · 0 评论 -
我自己写的atoi(). 字符串转整型.
我自己写的atoi(). 字符串转整型.enum ErrCode{ Err_OK, Err_InvalidParam, Err_Overflow, Err_Underflow,};ErrCode myAtoI(const char* pch, int&result){ // param checking. if (NULL == pch) return Err_Inval原创 2012-09-07 13:28:04 · 885 阅读 · 0 评论 -
从Stream中读取字符串长度,数值过大导致后期操作Crash
某字符串类型的记录,按该记录的定义,第一个DWORD表示该字符串的长度,后面是字符串内容。Load时,从Stream中读取该记录的过程:首先读出一个DWORD,保存为长度,然后malloc相应长度的内存,用来保存字符串。问题:stream中的内容被破坏,读出的字符串长度非常大,但是当前代码中对该数据的判断仅有:if (nlength >0 &&nlength *size原创 2012-11-26 16:02:54 · 1308 阅读 · 0 评论 -
Rotated array related program. 和旋转数组相关的几个程序
问题一:将一个具有n个元素的数组向右旋转i个位置。EXAMPLE:Input: 将(1, 2, 3, 4, 5, 6, 7, 8, 9)旋转3个位置Output: (7, 8, 9, 1, 2, 3, 4, 5, 6)问题二:(Cracking the coding interview 9.3)Given a sorted array of n integers that原创 2013-01-08 17:36:43 · 10779 阅读 · 0 评论