
剑指offer
文章平均质量分 60
duziliulang123
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
二维数组中的查找
每一次选取数组查找范围内右上角的那个数,如果该数字等于要查找的数,查找过程结束;如果该数字大于要查找的数,那么该列所有数都大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,那么该行所有数都小于要查找的数字,剔除这个数字所在的行。这样每一步都可以缩小查找范围,直到找到要查找的数字,或者查找范围为空。#includeusing namespace std;bool Find原创 2016-03-05 16:42:01 · 357 阅读 · 0 评论 -
旋转数组的最小数字
用二分法可以实现O(logn)的效率。用两个指针分别指向数组第一个元素和最后一个元素,第一个元素应该是大于等于最后一个元素的。找到数组中间的元素,如果该中间元素位于前面的递增子数组,那么它应该大于等于第一个元素,最小元素应该位于中间元素的后面,可以把第一个指针指向该中间元素,这样可以缩小寻找范围。如果中间元素位于后面的递增子数组,那么它应该小于最后一个元素,此时最小元素应该位于中间元素的前面,此时原创 2016-03-11 22:24:53 · 261 阅读 · 0 评论 -
二进制中1的个数
可以通过移位实现,不过有更简单的方法。将一个整数减去1之后,最右边的1变为0,其后面的0都变成了1,如果将结果与原来的整数相与,会把原整数最后一个1变为0,这样有多少个1只需要进行几次这样的操作。#includeusing namespace std;int NumberOf1(int n){ int count = 0; while (n) { ++count; n =原创 2016-03-12 11:03:22 · 214 阅读 · 0 评论 -
数值的整数次方
考虑到指数是负数的情况,考虑到底数为0的情况#includeusing namespace std;bool g_InvalidInput = false;bool equal(double num1, double num2){ if (((num1 - num2) > -0.0000001) && ((num1 - num2) < 0.00000001)) return原创 2016-03-12 11:59:56 · 202 阅读 · 0 评论 -
打印1到最大的n位数
考虑大数存储,最方便的是递归打印#includeusing namespace std;void PrintNumber(char *number){ bool isBeginning0 = true; int nLength = strlen(number); for (int i = 0; i < nLength; i++) { if (isBeginning0&&nu原创 2016-03-12 17:09:11 · 213 阅读 · 0 评论 -
带min函数的栈
#include#includeusing namespace std;templateclass StackWithMin{private: stack m_data; stack m_min;public: void pop(); void push(const T& value); const T& min()const;};templatevoid Sta原创 2016-04-05 16:13:49 · 317 阅读 · 0 评论 -
Z栈的压入,弹出序列
#includeusing namespace std;bool IsPopOrder(const int* pPush,const int* pPop,int nLength){ bool bPossible=false; if(pPush!=NULL&&pPop!=NULL&&nLength>0) { const int* pNextPush=pPush; const原创 2016-04-05 21:20:52 · 368 阅读 · 0 评论 -
在O(1)时间删除链表节点
将下一个节点的内容复制到要删除的那个节点上覆盖原来的内容,再把下一个节点删除,就相当于将当前要删除的节点删除了。#includeusing namespace std;struct ListNode{ int m_nValue; ListNode* m_pNext;};void DeleteNode(ListNode** pListHead, ListNode* pToBeD原创 2016-03-12 19:38:59 · 285 阅读 · 0 评论 -
调整数组顺序使奇数在前
用一个单独的函数来判断数字是否符合标准,利用函数指针进行操作。#includeusing namespace std;void Reorder(int *pData, unsigned int length, bool(*func)(int)){ if (pData == NULL || length == 0) return; int *pBegin = pData; in原创 2016-03-13 21:39:40 · 273 阅读 · 0 评论 -
二叉树的镜像
#include"BinaryTree.h"void MirrorRecursively(BinaryTreeNode* pNode){ if(pNode==NULL||pNode->m_pLeft==NULL&&pNode->m_pRight==NULL) return; BinaryTreeNode *pTemp=pNode->m_pLeft; pNode->m_pLeft=p原创 2016-04-05 15:20:46 · 326 阅读 · 0 评论 -
寻找子树
#include"BinaryTree.h"bool DoesTree1HasTree2(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2){ if(pRoot2==NULL) return true; if(pRoot1==NULL) return false; if(pRoot1->m_nValue!=pRoot2->m_nValu原创 2016-04-05 11:39:21 · 426 阅读 · 0 评论 -
替换空格
从字符串的后面开始复制和替换,首先准备两个指针,P1和P2,P1指向原始字符串的末尾,而P2指向替换后的字符串的末尾,向前移动指针P1,逐个把它指向的字符复制到P2指向的位置,知道碰到第一个空格为止,碰到第一个空格之后,把P1向前移动1格,在P2之前插入字符串“%20”,再接着向前复制。#includeusing namespace std;void ReplaceBlank(char原创 2016-03-05 23:50:26 · 254 阅读 · 0 评论 -
从尾到头打印链表
典型的后进先出,可以用栈实现这种顺序,每经过一个节点的时候,把该节点放到栈中,当遍历完整个链表后,再从栈顶开始逐个输出节点的值。#include#includeusing namespace std;struct ListNode{ int m_nKey; ListNode *m_pNext;};void AddToTail(ListNode** pHead,int val原创 2016-03-07 23:34:37 · 237 阅读 · 0 评论 -
重建二叉树
现根据前序遍历序列中的第一个数字创建根节点,接下来在中序遍历中找到根节点的位置,这样就能确定左右子树节点的数量。在前序遍历和中序遍历的序列中划分了左右子树节点的值之后,就可以递归地进行。#includeusing namespace std;struct BinaryTreeNode{ int m_nValue; BinaryTreeNode* m_pLeft; BinaryTr原创 2016-03-09 20:01:54 · 244 阅读 · 0 评论 -
两个栈实现队列
插入元素时压入stack1,删除元素时将stack1中的元素逐个弹出并压入stack2,元素在stack2中的顺序正好和原来在stack1中相反。stack2中的栈顶元素是最先进入的元素。#include#includeusing namespace std;templateclass CQueue{public: void appendTail(const T& node);原创 2016-03-09 20:53:23 · 235 阅读 · 0 评论 -
两个队列实现栈
#include#includeusing namespace std;templateclass CStack{public: void push(const T& element); T pop();private: queue queue1; queue queue2;};templatevoid CStack::push(const T& element)原创 2016-03-09 21:20:28 · 252 阅读 · 0 评论 -
快速排序递归算法
#includeusing namespace std;void Swap(int* x, int *y){ int tmp = *x; *x = *y; *y = tmp;}int Median(int* a, int left, int right){ int center = (left + right) / 2; if (a[left] > a[center])原创 2016-03-11 16:06:00 · 304 阅读 · 0 评论 -
快速排序的非递归写法
#include#includeusing namespace std;struct node{ int left; int right;};void Swap(int *x, int *y){ int tmp = *x; *x = *y; *y = tmp;}int Median(int *a, int left, int right){ int cente原创 2016-03-11 16:59:58 · 437 阅读 · 0 评论 -
快速排序小规模数据
#include#include#includeusing namespace std;static int CutOff = 10;void Swap(int *x, int * y){ int tmp = *x; *x = *y; *y = tmp;}int Median(int *a, int left, int right){ int center = (l原创 2016-03-11 17:52:15 · 453 阅读 · 0 评论 -
链表中倒数第k个节点
为了实现只遍历一次链表的目标,使用两个指针,第一个指针从链表头开始向前遍历走k-1步,第二个指针保持不动,从第k步开始,两个指针一起走,两个指针之间的距离保持在k-1,当第一个指针到达链表的尾部时,第二个指针正好在链表的倒数第k个节点位置。#includeusing namespace std;struct ListNode{ int m_nKey; ListNode *m_pNe原创 2016-03-13 22:08:07 · 313 阅读 · 0 评论