剑指offer笔记
小凡1991
我就是我
展开
-
剑指offer——面试题50:树中两个结点的最低公共祖先
题目:求树中两个结点的最低公共祖先,此树不是二叉树,并且没有指向父节点的指针。题目解析:如下图一颗普通二叉树假设输入结点 F 和 H。我们首先得到一条从根结点到树中某一结点的路径,这就要求在遍历的时候,有一个辅助内存来保存路径。比如我们用前序遍历的方法来得到从根结点到 H 的路径的过程是这样的:( 1 )遍历到 A,把 A 存放到路径中去,路径中只有一个结点 A;( 2原创 2016-12-15 14:48:49 · 393 阅读 · 0 评论 -
剑指offer——面试题35:第一次只出现一次的字符
char FirstNotRepeatingChar(char* pString){ if(pString == NULL) return '\0'; const int tableSize = 256; unsigned int hashTable[tableSize]; for(unsigned int i = 0; i<tableSize;原创 2016-12-07 16:24:47 · 322 阅读 · 0 评论 -
剑指offer——面试题34:丑数
bool IsUgly(int number){ while(number % 2 == 0) number /= 2; while(number % 3 == 0) number /= 3; while(number % 5 == 0) number /= 5; return (number == 1) ? t原创 2016-12-07 15:10:25 · 377 阅读 · 0 评论 -
剑指offer——面试题33:把数组排成最小的数
void PrintMinNumber(int* numbers, int length){ if(numbers == NULL || length <= 0) return; char** strNumbers = (char**)(new int[length]); for(int i = 0; i < length; ++i) {原创 2016-12-07 15:03:39 · 394 阅读 · 0 评论 -
剑指offer——面试题32:从1到n整数中1出现的次数
int NumberOf1Between1AndN_Solution1(unsigned int n){ int number = 0; for(unsigned int i = 1; i <= n; ++ i) number += NumberOf1(i); return number;}int NumberOf1(unsigned int n原创 2016-12-07 14:57:24 · 502 阅读 · 0 评论 -
剑指offer——面试题31:连续子数组的最大和
int FindGreatestSumOfSubArray(int *pData, int nLength){ if((pData == NULL) || (nLength <= 0)) { g_InvalidInput = true; return 0; } g_InvalidInput = false; int nC原创 2016-12-07 14:45:18 · 318 阅读 · 0 评论 -
剑指offer——面试题30:最小的k个数
void GetLeastNumbers_Solution1(int* input, int n, int* output, int k){ if(input == NULL || output == NULL || k > n || n <= 0 || k <= 0) return; int start = 0; int end = n - 1;原创 2016-12-04 17:30:30 · 353 阅读 · 0 评论 -
剑指offer——面试题29:数组中出现次数超过一半的数字
int MoreThanHalfNum_Solution1(int* numbers, int length){ if(CheckInvalidArray(numbers, length)) return 0; int middle = length >> 1; int start = 0; int end = length - 1;原创 2016-12-04 16:47:55 · 323 阅读 · 0 评论 -
剑指offer——面试题49:不使用atoi库函数实现把字符串转换成整数
下面是参考代码:enum Status {kValid = 0, kInvalid};int g_nStatus = kValid;int StrToInt(const char* str){ g_nStatus = kInvalid; long long num = 0; if(str != NULL && *str != '\0') {原创 2016-12-14 23:32:18 · 860 阅读 · 0 评论 -
剑指offer——面试题28:字符串的排列
void Permutation(char* pStr){ if(pStr == NULL) return; Permutation(pStr, pStr);}void Permutation(char* pStr, char* pBegin){ if(*pBegin == '\0') { printf("%s\n", p原创 2016-12-02 19:18:55 · 362 阅读 · 0 评论 -
剑指offer——面试题27:二叉搜索树与双向链表
BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree){ BinaryTreeNode *pLastNodeInList = NULL; ConvertNode(pRootOfTree, &pLastNodeInList); // pLastNodeInList指向双向链表的尾结点, // 我们需要返回头结点原创 2016-12-02 17:34:30 · 290 阅读 · 0 评论 -
剑指offer——面试题5:从尾到头打印链表
void PrintListReversingly_Iteratively(ListNode* pHead){ std::stack nodes; ListNode* pNode = pHead; while(pNode != NULL) { nodes.push(pNode); pNode = pNode->m_pNext;原创 2016-11-25 10:33:40 · 358 阅读 · 0 评论 -
剑指offer——面试题6:重建二叉树
BinaryTreeNode* Construct(int* preorder, int* inorder, int length){ if(preorder == NULL || inorder == NULL || length <= 0) return NULL; return ConstructCore(preorder, preorder + leng原创 2016-11-25 12:19:44 · 359 阅读 · 0 评论 -
剑指offer——面试题7:用两个栈实现队列
template void CQueue::appendTail(const T& element){ stack1.push(element);} template T CQueue::deleteHead(){ if(stack2.size()<= 0) { while(stack1.size()>0) {原创 2016-11-25 13:14:44 · 397 阅读 · 0 评论 -
剑指offer——面试题45:圆圈中最后剩下的数字
int LastRemaining(unsigned int n, unsigned int m){ if(n < 1 || m < 1) return -1; unsigned int i = 0; list numbers; for(i = 0; i < n; ++ i) numbers.push_back(i);原创 2016-12-14 16:45:27 · 860 阅读 · 0 评论 -
剑指offer——面试题12:打印1到最大的n位数
void Print1ToMaxOfNDigits_1(int n){ if(n <= 0) return; char *number = new char[n + 1]; memset(number, '0', n); number[n] = '\0'; while(!Increment(number)) {原创 2016-11-27 17:30:33 · 363 阅读 · 0 评论 -
剑指offer——面试题11:数值的整数次方
bool g_InvalidInput = false;double Power(double base, int exponent){ g_InvalidInput = false; if(equal(base, 0.0) && exponent < 0) { g_InvalidInput = true; return 0.0;原创 2016-11-26 21:48:05 · 350 阅读 · 0 评论 -
剑指offer——面试题10:位运算之二进制中1的个数
int NumberOf1_Solution2(int n){ int count = 0; while (n) { ++ count; n = (n - 1) & n; } return count;}原创 2016-11-26 20:45:16 · 415 阅读 · 0 评论 -
剑指offer——面试题39:二叉树的深度
int TreeDepth(BinaryTreeNode* pRoot){ if(pRoot == NULL) return 0; int nLeft = TreeDepth(pRoot->m_pLeft); int nRight = TreeDepth(pRoot->m_pRight); return (nLeft > nRight) ? (原创 2016-12-08 19:53:31 · 603 阅读 · 0 评论 -
剑指offer——面试题38:数字在排序数组中出现的次数
int GetFirstK(int* data, int length, int k, int start, int end){ if(start > end) return -1; int middleIndex = (start + end) / 2; int middleData = data[middleIndex]; if(middl原创 2016-12-08 19:47:46 · 250 阅读 · 0 评论 -
剑指offer——面试题37:两个链表的第一个公共节点
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2){ // 得到两个链表的长度 unsigned int nLength1 = GetListLength(pHead1); unsigned int nLength2 = GetListLength(pHead2); int nLen原创 2016-12-08 19:41:58 · 439 阅读 · 0 评论 -
剑指offer——面试题9:斐波那契数列
// ====================方法2:循环====================long long Fibonacci_Solution2(unsigned n){ int result[2] = {0, 1}; if(n < 2) return result[n]; long long fibNMinusOne = 1;原创 2016-11-25 21:49:39 · 308 阅读 · 0 评论 -
剑指offer——面试题8:旋转数组的最小数字
int Min(int* numbers, int length){ if(numbers == NULL || length <= 0) throw new std::exception("Invalid parameters"); int index1 = 0; int index2 = length - 1; int indexMid =原创 2016-11-25 21:12:50 · 363 阅读 · 0 评论 -
剑指offer——面试题36:数组中的逆序对
int InversePairs(int* data, int length){ if(data == NULL || length < 0) return 0; int* copy = new int[length]; for(int i = 0; i < length; ++ i) copy[i] = data[i]; in原创 2016-12-07 21:44:35 · 658 阅读 · 0 评论 -
剑指offer——面试题26:复杂链表的复制
void CloneNodes(ComplexListNode* pHead){ ComplexListNode* pNode = pHead; while(pNode != NULL) { ComplexListNode* pCloned = new ComplexListNode(); pCloned->m_nValue = pNode原创 2016-12-02 17:02:23 · 255 阅读 · 0 评论 -
剑指offer——面试题25:二叉树中和为某一值的路径
void FindPath(BinaryTreeNode* pRoot, int expectedSum){ if(pRoot == NULL) return; std::vector path; int currentSum = 0; FindPath(pRoot, expectedSum, path, currentSum);}void原创 2016-12-02 16:22:34 · 233 阅读 · 0 评论 -
剑指offer——面试题19:二叉树的镜像
void MirrorRecursively(BinaryTreeNode *pNode){ if((pNode == NULL) || (pNode->m_pLeft == NULL && pNode->m_pRight)) return; BinaryTreeNode *pTemp = pNode->m_pLeft; pNode->m_pLeft =原创 2016-11-29 22:12:52 · 273 阅读 · 0 评论 -
剑指offer——面试题18:树的子结构
bool HasSubtree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2){ bool result = false; if(pRoot1 != NULL && pRoot2 != NULL) { if(pRoot1->m_nValue == pRoot2->m_nValue)原创 2016-11-29 21:35:37 · 262 阅读 · 0 评论 -
剑指offer——面试题17:合并两个排序的链表
ListNode* Merge(ListNode* pHead1, ListNode* pHead2){ if(pHead1 == NULL) return pHead2; else if(pHead2 == NULL) return pHead1; ListNode* pMergedHead = NULL; if(pHead1原创 2016-11-29 17:13:28 · 270 阅读 · 0 评论 -
剑指offer——面试题16:反转链表
ListNode* ReverseList(ListNode* pHead){ ListNode* pReversedHead = NULL; ListNode* pNode = pHead; ListNode* pPrev = NULL; while(pNode != NULL) { ListNode* pNext = pNode->m_原创 2016-11-29 16:24:59 · 335 阅读 · 0 评论 -
剑指offer——面试题15:链表中倒数第k个结点
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k){ if(pListHead == NULL || k == 0) return NULL; ListNode *pAhead = pListHead; ListNode *pBehind = NULL; for(unsign原创 2016-11-29 15:07:48 · 245 阅读 · 0 评论 -
剑指offer——面试题14:调整数组顺序使奇数位于偶数前面
void ReorderOddEven_1(int *pData, unsigned int length){ if(pData == NULL || length == 0) return; int *pBegin = pData; int *pEnd = pData + length - 1; while(pBegin < pEnd)原创 2016-11-29 12:54:58 · 300 阅读 · 0 评论 -
剑指offer——面试题13:在O(1)时间删除链表结点
void DeleteNode(ListNode** pListHead, ListNode* pToBeDeleted){ if(!pListHead || !pToBeDeleted) return; // 要删除的结点不是尾结点 if(pToBeDeleted->m_pNext != NULL) { ListNode* pN原创 2016-11-29 11:33:04 · 337 阅读 · 0 评论 -
剑指offer——面试题4:替换空格
题目:实现一个函数,把字符串中的每个空格替换成"%20”。例如输入“We are happy.”,则输出"We%20are%20happy."。思路:先遍历一次字符串,就能统计出字符串中空格的总数,并能由此计算出替换之后的字符串的总长度。没替换一个空格,长度加2,因此替换以后字符串的长度等于原来的长度加上2乘以空格的书目。以“We are happy.”为例。首先准备两个指针P1和原创 2016-07-08 20:28:49 · 743 阅读 · 0 评论 -
剑指offer——面试题3:二维数组中的查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。思路:选取数组中右上角的数字,如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,剔除这个数字所在的行。这样每一步缩小查找的范围,直到找到要查找的数原创 2016-07-08 13:25:21 · 546 阅读 · 0 评论 -
剑指offer——C++面试需要的基础知识
面试官对C++基础知识第一种面试方式是直接口头询问对C++概念的理解,面试官特别喜欢了解应聘者对关键字的理解程度。例如:在C++中,有哪4个与类型转换相关的关键字?这些关键字各有什么特点,应该在什么场合下使用?这类题目中,size of是经常被问到的一个概念。例如:定义一个空的类型,里面没有任何成员变量和成员函数。对该类型求size of,得到的结果是多少?答案是1,因为空类型的实例中原创 2016-06-09 22:25:43 · 1679 阅读 · 0 评论 -
剑指offer——面试题20:顺时针打印矩阵
void PrintMatrixClockwisely(int** numbers, int columns, int rows){ if(numbers == NULL || columns <= 0 || rows <= 0) return; int start = 0; while(columns > start * 2 && rows > st原创 2016-11-29 22:51:15 · 309 阅读 · 0 评论 -
剑指offer——面试题40:数组中只出现一次的数字
void FindNumsAppearOnce(int data[], int length, int* num1, int* num2){ if (data == NULL || length < 2) return; int resultExclusiveOR = 0; for (int i = 0; i < length; ++ i)原创 2016-12-11 20:35:23 · 436 阅读 · 0 评论 -
剑指offer——面试题41:和为s的两个数字VS和为s的连续正数序列
bool FindNumbersWithSum(int data[], int length, int sum, int* num1, int* num2){ bool found = false; if(length < 1 || num1 == NULL || num2 == NULL) return foun原创 2016-12-11 20:55:39 · 498 阅读 · 0 评论 -
剑指offer——面试题48:用C++设计一个不能被继承的类
class SealedClass1{public: static SealedClass1* GetInstance() { return new SealedClass1(); } static void DeleteInstance( SealedClass1* pInstance) { delete pIns原创 2016-12-11 23:17:38 · 431 阅读 · 0 评论