
剑指offer
hjhomw
今天的苦逼是为了不这样一直苦逼下去,坚持,坚持,坚持!
展开
-
和为s的正数序列
代码:#include <iostream>using namespace std;void FindSequentialNumbersWithSum(int* array, int nLength,int sum){ int first_index = 0; int last_index = 1; while (first_index <= last_index && la原创 2016-11-02 21:12:58 · 326 阅读 · 0 评论 -
二叉搜索树的后序遍历序列
代码://二叉搜索树的后序遍历bool postOrderTraversalOfBST(int array[], int length){ if (array == nullptr || length <= 0) return false; int root = array[length - 1]; int i = 0; for (; i < le原创 2016-10-20 10:28:31 · 240 阅读 · 0 评论 -
顺时针打印矩阵
代码:#include <iostream>#include <cstdlib>using namespace std;//打印一圈矩阵void PrintMatrixCircle(int** numbers, int columns, int rows, int start){ int endX = columns - 1 - start; int endY = rows -原创 2016-10-19 16:08:52 · 225 阅读 · 0 评论 -
二叉树的镜像
代码:struct BinaryTreeNode{ int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};void MirrorRecursive(BinaryTreeNode* pTree){ if (pTree == nullptr || (pTree->m_pLeft == nu原创 2016-10-18 16:02:30 · 217 阅读 · 0 评论 -
求两个链表的第一个公共结点
代码://结点struct ListNode{ int m_val; ListNode* m_pNext;};//第一种方法,需要两个辅助栈ListNode* FirstCommonNode(ListNode* & pList1, ListNode* & pList2){ if (pList1 == nullptr || pList2 == nullptr)原创 2016-10-27 21:15:59 · 306 阅读 · 0 评论 -
翻转链表
代码:struct ListNode{ int m_nValue; ListNode* m_pNext;};ListNode* ReverseList(ListNode *pHead){ ListNode* pReverseList = nullptr; ListNode* pCur = pHead; ListNode* pPrev = nullptr;原创 2016-10-18 13:22:32 · 196 阅读 · 0 评论 -
链表中倒数第k个结点
代码:struct ListNode{ int m_nValue; ListNode* m_pNext;};ListNode* findKthtoTail(ListNode* pHead, int k){ if (pHead == nullptr || k <= 0) return nullptr; ListNode *p1 = pHead, *原创 2016-10-18 11:00:41 · 186 阅读 · 0 评论 -
数组中的逆序对
代码:#include <iostream>using namespace std;//逆序对int InversePairCore(int* pArray, int* pCopy, int start, int end){ if (start == end) { pCopy[start] = pArray[start]; return 0;转载 2016-10-27 20:39:27 · 283 阅读 · 0 评论 -
调整数组顺序使奇数位于偶数前面
代码:#include <iostream>using namespace std;//判断是否为奇数bool isOdd(int num){ return (num & 1) == 1;}void frontOddbackEven(int *pData, int length, bool(*fun)(int)){ if (pData == nullptr || lengt原创 2016-10-18 10:14:04 · 231 阅读 · 0 评论 -
打印1到最大的n位数
代码:#include <iostream>using namespace std;//打印数字void printNumber(char* number) //前面为0的数字不打印{ int len = strlen(number); int i = 0; while (number[i] == '0') i++; for (int j = i原创 2016-10-17 21:40:46 · 201 阅读 · 0 评论 -
包含min函数的栈
代码:#include <iostream>#include <stack>using namespace std;template <typename T> class StackWithMin{public: StackWithMin(); ~StackWithMin(); const T& top() const; void push(const T& v原创 2016-10-19 21:20:56 · 242 阅读 · 0 评论 -
栈的压入,弹出序列
代码:#include <iostream>#include <stack>using namespace std;bool StackPushPopOrder(const int* pPush, const int* pPop, const int length){ bool bPossible = false; if (pPush == nullptr || pPop ==原创 2016-10-19 22:17:05 · 271 阅读 · 0 评论 -
和为s的两个数字
代码:#include <iostream>using namespace std;void FindTwoNumbersWithSum(int* Array, int nLength, int* number1, int* number2, int sum){ int pHead = 0; int pEnd = nLength - 1; while (pHead < pE原创 2016-11-02 20:43:17 · 295 阅读 · 0 评论 -
数组中只出现一次的数字
代码:#include <iostream>using namespace std;int FindFirstIndexOf1(int number){ int index_bits = 0; while (((number & 1) == 0) && (index_bits < 8 * sizeof(int))) { number = number >>转载 2016-11-02 20:24:52 · 256 阅读 · 0 评论 -
判断平衡二叉树
代码://求二叉树的深度int BSTDepth(TreeNode* pRoot){ if (pRoot == nullptr) return 0; int LeftDepth = BSTDepth(pRoot->m_pLeft); int RightDepth = BSTDepth(pRoot->m_pRight); return LeftDept转载 2016-11-02 19:25:52 · 308 阅读 · 0 评论 -
数字在排序数组中出现的次数
代码:#include <iostream>using namespace std;//找到第一个k的下标int GetFirstIndex(int* pArray,int k, int start, int end){ int mid = 0; while (start <= end)原创 2016-11-01 22:12:22 · 274 阅读 · 0 评论 -
字符串的全排列
代码:#include <iostream>#include <vector>using namespace std;void Permutation(char* pStr, char* pBegin){ if ( *pBegin == '\0') { cout << pStr << "\t"; } else { for (原创 2016-10-20 22:09:57 · 267 阅读 · 0 评论 -
二叉搜索树与双向链表
代码:struct BinaryTreeNode{ int m_val; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};//递归方法BinaryTreeNode* BSTConvertToTwoWayList(BinaryTreeNode* pRoot){ BinaryTreeNode* pLastNo原创 2016-10-20 20:43:48 · 199 阅读 · 0 评论 -
复杂链表的复制
代码://复制复杂链表struct ComplexListNode{ int value; ComplexListNode* pNext; ComplexListNode* pSibling;};//复制链表void CopyList(ComplexListNode* pComplextListHead){ ComplexListNode* pNode =原创 2016-10-20 19:14:19 · 269 阅读 · 0 评论 -
二叉树中和为某一值的路径
代码:struct BinaryTreeNode{ int m_value; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};void findPath(BinaryTreeNode* pRoot, int sum, vector<int>& path, int currentSum){ currentSu原创 2016-10-20 16:29:43 · 233 阅读 · 0 评论 -
从上往下打印二叉树
代码:struct BinaryTreeNode{ int m_val; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};void PrintUpToBottom(BinaryTreeNode* pTree){ queue<BinaryTreeNode*> treeQue; if (pTree ==原创 2016-10-19 22:39:53 · 204 阅读 · 0 评论 -
第一个只出现一次的字符
代码:#include <iostream>using namespace std;char FindFirstNotRepeatingChar(char* pString){ if (pString == nullptr) return '\0'; const int size = 256; //char 是8个bit的类型,总共有256个字符 int转载 2016-10-27 14:40:30 · 220 阅读 · 0 评论 -
丑数
代码:#include <iostream>using namespace std;//找出三个数中的最小数int min(int number1, int number2, int number3) { int min = (number1 < number2) ? number1 : number2; return (min < number3) ? min : numbe转载 2016-10-27 14:16:48 · 293 阅读 · 0 评论 -
数值的整数次方
include using namespace std;bool equal(double num1, double num2) { if ((num1 - num2> -0.0000001) && (num1-num2 < 0.0000001)) return true;return false;}double PowerExponent(double base,原创 2016-10-17 18:21:53 · 221 阅读 · 0 评论 -
扑克牌的顺子
代码:#include <iostream>using namespace std;int compare(const void* arg1, const void* arg2){ return *(int*)arg1 - *(int*)arg2;}bool IsContinusCards(int* pCards, int nLength){ if (pCards == nul转载 2016-11-03 21:24:34 · 363 阅读 · 0 评论 -
n个骰子的点数之和
分析: 动态规划的思想: 1.现在变量有:骰子个数,点数和。当有k个骰子,点数和为n时,出现次数记为f(k,n)。那与k-1个骰子阶段之间的关系是怎样的? 2.当我有k-1个骰子时,再增加一个骰子,这个骰子的点数只可能为1、2、3、4、5或6。那k个骰子得到点数和为n的情况有: (k-1,n-1):第k个骰子投了点数1 (k-1,n-2):第k个骰子投了点数2 (k-1,n-3):第k个转载 2016-11-03 16:38:58 · 728 阅读 · 0 评论 -
左旋字符串
代码:#include <iostream>using namespace std; //翻转字符数组void Reverse(char* pBegin, char* pEnd){ if (pBegin == nullptr || pEnd == nullptr) return; while (pBegin < pEnd) { char te原创 2016-11-03 14:24:17 · 291 阅读 · 0 评论 -
翻转单词顺序
代码:#include <iostream>using namespace std; //翻转字符数组void Reverse(char* pBegin, char* pEnd){ if (pBegin == nullptr || pEnd == nullptr) return; while (pBegin < pEnd) { char te原创 2016-11-02 22:36:55 · 470 阅读 · 0 评论 -
最小的k个数
解法一: 若输入数组可以修改,可以基于快速排序的思想找数组 第k个大小的数的位置,返回[0~k-1]位置的数即可。解法二:typedef multiset<int, greater<int>> intSet;typedef multiset<int, greater<int>>::iterator iterSet;void GetLeastKNumbers(const vector<int>&原创 2016-10-21 15:48:00 · 210 阅读 · 0 评论 -
数组中出现次数超过一半的数字
代码:#include <iostream>#include <random>using namespace std;int RandomRange(int start, int end){ uniform_int_distribution<int> u(start, end); default_random_engine e; return u(e);}int Part原创 2016-10-21 15:03:14 · 217 阅读 · 0 评论 -
八皇后问题
代码:#include <iostream>using namespace std;//八皇后void eightQueue(int* colunmnIndex, int k ,int length){ if (k == length) { for (int i = 0 ; i < length; i++) { for (原创 2016-10-21 11:13:16 · 245 阅读 · 0 评论 -
用两个栈实现队列
题目:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。代码:#include <iostream>#include <stack>#include <exception>using namespace std;template<typename T> class CQueue{publi原创 2016-09-10 20:37:59 · 325 阅读 · 0 评论 -
替换空格
题目:请实现一个函数,把字符串中的每一个空格替换成”%20”。例如输入”we are happy.”,则输出”we%20are%20happy.”。分析: 将长度为1的空格替换为长度为3的“%20”,字符串的长度变长。如果允许我们开辟一个新的数组来存放替换空格后的字符串,那么这道题目就非常简 单。设置两个指针分别指向新旧字符串首元素,遍历原字符串,如果碰到空格就在新字符串上填入“%20”,否则就复原创 2016-09-10 16:07:57 · 325 阅读 · 0 评论 -
圆圈中最后剩下的数字
代码:#include <iostream>#include <list>using namespace std;int FindLastNumber(int n, int m){ if (n < 1 || m < 1) return -1; //用list模拟循环链表 list<int> numbers; for (int i = 0; i < n;转载 2016-11-04 09:57:41 · 329 阅读 · 0 评论 -
求1+2+...+n
代码:#include <iostream>#include <list>using namespace std;//方法一,利用构造函数求解class A{public: A() { ++n; sum += n; } static void Reset() { n = 0; sum转载 2016-11-04 16:33:47 · 400 阅读 · 0 评论 -
把数组排成最小的数
代码:#define _CRT_SECURE_NO_WARNINGS#include <iostream>using namespace std;const int g_max_length = 10; // 0~9char* g_str_combine1 = new char[2 * g_max_length + 1];char* g_str_combine2 = new char[2 *转载 2016-10-27 11:30:04 · 322 阅读 · 0 评论 -
二进制中1的个数
代码:#include <iostream>using namespace std;/*//右移有可能导致死循环int Numberof1(int n) //循环次数等于整数二进制的位数{int count = 0;while (n){if (n&1)count++;n = n>>1;}return count;}//常规解法int Numberof1(int n) //原创 2016-10-16 22:47:49 · 185 阅读 · 0 评论 -
旋转数组的最小数字
代码:#include <iostream>using namespace std;int MinInOrder(int num[], int index1, int index2){ int min_num = num[index1]; while (index1 < index2) { ++index1; if (min_num > nu原创 2016-10-16 21:39:49 · 212 阅读 · 0 评论 -
Fibonacii数列
代码:#include <iostream>using namespace std;//迭代求解斐波那契数列int Fibonacci(int n){ if (n <= 0) throw new exception("Invalid Parameters"); if (n == 1 || n == 2) return 1; int Fib_1原创 2016-10-16 22:08:58 · 598 阅读 · 0 评论 -
从1到n整数中1出现的次数
代码:#define _CRT_SECURE_NO_WARNINGS#include <iostream>using namespace std;int PowerBase10(int n){ int result = 1; for (int i = 0; i < n; i++) { result *= 10; } return resu转载 2016-10-26 17:02:47 · 240 阅读 · 0 评论