├── .gitignore ├── README.md ├── ch1 free ├── 两串旋转练习题 │ ├── Rotation.cpp │ └── 捕获.PNG └── 二叉树打印练习题 │ ├── TreePrinter.cpp │ └── 捕获.PNG ├── ch10 概率 ├── 蚂蚁习题 │ ├── Ants.cpp │ └── 捕获.PNG ├── 足球比赛练习题 │ ├── Championship.cpp │ └── 捕获.PNG ├── 随机01练习题 │ ├── Random01.cpp │ └── 捕获.PNG ├── 随机函数练习题 │ ├── Random7.cpp │ └── 捕获.PNG ├── 随机区间函数练习题 │ ├── RandomSeg.cpp │ └── 捕获.PNG └── 随机数组打印练习题 │ ├── RandomPrint.cpp │ └── 捕获.PNG ├── ch11 大数据 └── 哈希函数.PNG ├── ch12 动态规划 ├── 01背包练习题 │ ├── Backpack.cpp │ └── 捕获.PNG ├── LCS练习题 │ ├── LCS.cpp │ └── 捕获.PNG ├── LIS练习题 │ ├── LongestIncreasingSubsequence.cpp │ └── 捕获.PNG ├── 台阶问题练习题 │ ├── GoUpstairs.cpp │ └── 捕获.PNG ├── 找零钱练习题 │ ├── Exchange.cpp │ └── 捕获.PNG ├── 最优编辑练习题 │ ├── MinCost.cpp │ └── 捕获.PNG └── 矩阵最小路径和练习题 │ ├── MinimumPath.cpp │ └── 捕获.PNG ├── ch2 排序 ├── BubbleSort.cpp ├── CountingSort.cpp ├── HeapSort.cpp ├── InsertionSort.cpp ├── MergeSort.cpp ├── QuickSort.cpp ├── RadixSort.cpp ├── SelectionSort.cpp ├── ShellSort.cpp └── 经典例题 │ ├── 三色排序练习题 │ ├── ThreeColor.cpp │ └── 捕获.PNG │ ├── 小范围排序练习题 │ ├── ScaleSort.cpp │ └── 捕获.PNG │ ├── 最短子数组练习题 │ ├── Subsequence.cpp │ └── 捕获.PNG │ ├── 有序数组合并练习题 │ ├── Merge.cpp │ └── 捕获.PNG │ ├── 有序矩阵查找练习题 │ ├── Finder.cpp │ └── 捕获.PNG │ ├── 相邻两数最大差值练习题 │ ├── Gap.cpp │ └── 捕获.PNG │ └── 重复值判断练习题 │ ├── Checker.cpp │ └── 捕获.PNG ├── ch3 字符串 ├── 句子的逆序练习题 │ ├── Reverse.cpp │ └── 捕获.PNG ├── 合法括号序列判断练习题 │ ├── Parenthesis.cpp │ └── 捕获.PNG ├── 字符串移位练习题 │ ├── Translation.cpp │ └── 捕获.PNG ├── 拓扑结构相同子树练习题 │ ├── IdenticalTree.cpp │ └── 捕获.PNG ├── 拼接最小字典序练习题 │ ├── Prior.cpp │ └── 捕获.PNG ├── 最长无重复字符子串练习题 │ ├── DistinctSubstring.cpp │ └── 捕获.PNG ├── 空格替换练习题 │ ├── Replacement.cpp │ └── 捕获.PNG └── 词语变形练习题 │ ├── Transform.cpp │ └── 捕获.PNG ├── ch4 队列和栈 ├── 双栈排序练习题 │ ├── TwoStacks.cpp │ └── 捕获.PNG ├── 双栈队列练习题 │ ├── TwoStack.cpp │ └── 捕获.PNG ├── 可查询最值的栈练习题 │ ├── Solution.cpp │ └── 捕获.PNG ├── 数组变树练习题 │ ├── MaxTree.cpp │ └── 捕获.PNG ├── 栈的反转练习题 │ ├── StackReverse.cpp │ └── 捕获.PNG └── 滑动窗口练习题 │ ├── SlideWindow.cpp │ └── 捕获.PNG ├── ch5 链表 ├── 单链表相交判断练习题 │ ├── ChkIntersection.cpp │ └── 捕获.PNG ├── 复杂链表的复制练习题 │ ├── Solution.cpp │ └── 捕获.PNG ├── 打印两个链表的公共值练习题 │ ├── Common.cpp │ └── 捕获.PNG ├── 无环单链表判相交练习题 │ ├── CheckIntersect.cpp │ └── 捕获.PNG ├── 有环单链表相交判断练习题 │ ├── ChkIntersection.cpp │ └── 捕获.PNG ├── 环形链表插值练习题 │ ├── InsertValue.cpp │ └── 捕获.PNG ├── 访问单个节点的删除练习题 │ ├── Remove.cpp │ └── 捕获.PNG ├── 链表判环练习题 │ ├── ChkLoop.cpp │ └── 捕获.PNG ├── 链表指定值清除练习题 │ ├── ClearValue.cpp │ └── 捕获.PNG ├── 链表的k逆序练习题 │ ├── KInverse.cpp │ └── 捕获.PNG ├── 链表的分化练习题 │ ├── Divide.cpp │ └── 捕获.PNG ├── 链表的回文结构练习题 │ ├── Palindrome.cpp │ └── 捕获.PNG └── 链表相交小结.txt ├── ch6 二分搜索 ├── 元素最左出现练习题 │ ├── LeftMostAppearance.cpp │ └── 捕获.PNG ├── 完全二叉树计数练习题 │ ├── CountNodes.cpp │ └── 捕获.PNG ├── 局部最小值位置练习题 │ ├── Solution.cpp │ └── 捕获.PNG ├── 循环有序数组最小值练习题 │ ├── MinValue.cpp │ └── 捕获.PNG ├── 快速N次方练习题 │ ├── QuickPower.cpp │ └── 捕获.PNG └── 最左原位 │ ├── Find.cpp │ └── 捕获.PNG ├── ch7 二叉树 ├── 二叉树的序列化练习题 │ ├── TreeToString.cpp │ └── 捕获.PNG ├── 二叉树的打印练习题 │ ├── TreePrinter.cpp │ └── 捕获.PNG ├── 完全二叉树判断练习题 │ ├── CheckCompletion.cpp │ └── 捕获.PNG ├── 寻找错误结点练习题 │ ├── FindErrorNod.cpp │ └── 捕获.PNG ├── 平衡二叉树判断练习题 │ ├── CheckBalance.cpp │ └── 捕获.PNG ├── 折纸练习题 │ ├── FoldPaper.cpp │ └── 捕获.PNG ├── 最大二叉搜索子树练习题 │ ├── MaxSubtree.cpp │ └── 捕获.PNG ├── 树上最远距离练习题 │ ├── LongestDistance.cpp │ └── 捕获.PNG ├── 递归二叉树的序列打印练习题 │ ├── TreeToSequence.cpp │ └── 捕获.PNG └── 非递归二叉树的序列打印练习题 │ ├── TreeToSequence.cpp │ ├── 捕获.PNG │ ├── 非递归中序.PNG │ ├── 非递归先序.PNG │ ├── 非递归后序1.PNG │ └── 非递归后序2.PNG ├── ch8 位运算 ├── 交换练习题 │ ├── Swap.cpp │ └── 捕获.PNG ├── 寻找奇数出现II练习题 │ ├── OddAppearanc.cpp │ └── 捕获.PNG ├── 寻找奇数出现练习题 │ ├── OddAppearanc.cpp │ └── 捕获.PNG └── 比较练习题 │ ├── Compare.cpp │ └── 捕获.PNG ├── ch9 排列与组合 ├── 二叉树统计练习题 │ ├── TreeCount.cpp │ └── 捕获.PNG ├── 分糖果练习题 │ ├── Distribution.cpp │ └── 捕获.PNG ├── 卡特兰数重要公式I.PNG ├── 卡特兰数重要公式II.PNG ├── 孤傲的A练习题 │ ├── LonelyA.cpp │ └── 捕获.PNG ├── 括号序列练习题 │ ├── Parenthesis.cpp │ └── 捕获.PNG ├── 排队买票练习题 │ ├── BuyTickets.cpp │ └── 捕获.PNG ├── 方格移动练习题 │ ├── Robot.cpp │ └── 捕获.PNG ├── 站队问题练习题 │ ├── StandInLine.cpp │ └── 捕获.PNG ├── 进出栈练习题 │ ├── Stack.cpp │ └── 捕获.PNG ├── 错装信封练习题 │ ├── CombineByMistake.cpp │ └── 捕获.PNG └── 高矮排列练习题 │ ├── HighAndShort.cpp │ └── 捕获.PNG └── 四则运算.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # -BAT- 2 | 牛客网/直通BAT面试算法精讲课源码 3 | -------------------------------------------------------------------------------- /ch1 free/两串旋转练习题/Rotation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Rotation { 5 | public: 6 | bool chkRotation(string A, int lena, string B, int lenb) 7 | { 8 | string sum=A+A; 9 | string::size_type pos=sum.find(B); 10 | if(pos!=string::npos) 11 | return true; 12 | else 13 | return false; 14 | } 15 | }; -------------------------------------------------------------------------------- /ch1 free/两串旋转练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch1 free/两串旋转练习题/捕获.PNG -------------------------------------------------------------------------------- /ch1 free/二叉树打印练习题/TreePrinter.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch1 free/二叉树打印练习题/TreePrinter.cpp -------------------------------------------------------------------------------- /ch1 free/二叉树打印练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch1 free/二叉树打印练习题/捕获.PNG -------------------------------------------------------------------------------- /ch10 概率/蚂蚁习题/Ants.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define Mod 1000000007 5 | class Ants { 6 | public: 7 | vector collision(int n) 8 | { 9 | int down=pow(2.0,n),up=down-2; 10 | int temp=gcd(up,down); 11 | vector res; 12 | res.push_back(up/temp); 13 | res.push_back(down/temp); 14 | return res; 15 | } 16 | int gcd(int x,int y) 17 | { 18 | return (!y)?x:gcd(y,x%y); 19 | } 20 | }; -------------------------------------------------------------------------------- /ch10 概率/蚂蚁习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/蚂蚁习题/捕获.PNG -------------------------------------------------------------------------------- /ch10 概率/足球比赛练习题/Championship.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define Mod 1000000007 5 | class Championship { 6 | public: 7 | vector calc(int k) 8 | { 9 | vector res; 10 | int up=1,down=1,i=2*k-1; 11 | while(i>0) 12 | { 13 | down*=i; 14 | i=i-2; 15 | } 16 | i=k+1; 17 | while(i>2) 18 | { 19 | up*=i; 20 | i--; 21 | } 22 | int Com=gcd(down-up,down); 23 | res.push_back((down-up)/Com); 24 | res.push_back(down/Com); 25 | return res; 26 | } 27 | int gcd(int x, int y) 28 | { 29 | return (!y)?x:gcd(y,x%y); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /ch10 概率/足球比赛练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/足球比赛练习题/捕获.PNG -------------------------------------------------------------------------------- /ch10 概率/随机01练习题/Random01.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/随机01练习题/Random01.cpp -------------------------------------------------------------------------------- /ch10 概率/随机01练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/随机01练习题/捕获.PNG -------------------------------------------------------------------------------- /ch10 概率/随机函数练习题/Random7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/随机函数练习题/Random7.cpp -------------------------------------------------------------------------------- /ch10 概率/随机函数练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/随机函数练习题/捕获.PNG -------------------------------------------------------------------------------- /ch10 概率/随机区间函数练习题/RandomSeg.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/随机区间函数练习题/RandomSeg.cpp -------------------------------------------------------------------------------- /ch10 概率/随机区间函数练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch10 概率/随机区间函数练习题/捕获.PNG -------------------------------------------------------------------------------- /ch10 概率/随机数组打印练习题/RandomPrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class RandomPrint { 5 | public: 6 | vector print(vector arr, int N, int M) 7 | { 8 | vector res; 9 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | class Backpack 5 | { 6 | public: 7 | int maxValue(vector w, vector v, int n, int cap) 8 | { 9 | int **dp=new int*[n]; 10 | for(int i=0;i=w[0]) 17 | dp[0][i]=v[0]; 18 | else 19 | dp[0][i]=0; 20 | } 21 | for(int i=1;i=0) 25 | dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]); 26 | else 27 | dp[i][j]=dp[i-1][j]; 28 | } 29 | int res=dp[n-1][cap]; 30 | for(int i=0;i arr(a,a+8); 43 | vector brr(b,b+8); 44 | Backpack B; 45 | cout< 2 | #include 3 | using namespace std; 4 | class LCS { 5 | public: 6 | int findLCS(string A, int n, string B, int m) 7 | { 8 | int **dp=new int*[n]; 9 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | class LongestIncreasingSubsequence { 5 | public: 6 | int getLIS(vector A, int n) 7 | { 8 | int *dp=new int[n],res=0; 9 | dp[0]=1; 10 | for(int i=1;imax) 16 | max=dp[j]; 17 | j++; 18 | } 19 | dp[i]=max+1; 20 | } 21 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | #define Mod 1000000007 5 | class GoUpstairs { 6 | public: 7 | int countWays(int n) 8 | { 9 | int *dp=new int[n]; 10 | dp[0]=1; 11 | dp[1]=2; 12 | for(int i=2;i 2 | #include 3 | using namespace std; 4 | class MinimumPath { 5 | public: 6 | int getMin(vector > map, int n, int m) 7 | { 8 | int **dp=new int *[n]; 9 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | class ThreeColor { 5 | public: 6 | vector sortThreeColor(vector &A, int n) 7 | { 8 | int f,r,i,temp; 9 | for(i=f=0,r=n-1;i<=r;i++) 10 | { 11 | if(A[i]==0) 12 | { 13 | temp=A[f]; 14 | A[f]=A[i]; 15 | A[i]=temp; 16 | f++; 17 | } 18 | if(A[i]==2) 19 | { 20 | temp=A[r]; 21 | A[r]=A[i]; 22 | A[i]=temp; 23 | r--; 24 | i--; 25 | } 26 | } 27 | return A; 28 | } 29 | }; 30 | int main() 31 | { 32 | int a[6]={1,2,0,2}; 33 | vector b(a,a+4); 34 | ThreeColor c; 35 | c.sortThreeColor(b,4); 36 | for(int i=0;i<4;i++) 37 | cout< 2 | #include 3 | using namespace std; 4 | class Subsequence { 5 | public: 6 | int shortestSubsequence(vector A, int n) 7 | { 8 | int max=A[0],min=A[n-1],i,rd1,rd2; 9 | for(i=1,rd1=0;i=max) 12 | max=A[i]; 13 | else 14 | rd1=i; 15 | } 16 | for(i=n-2,rd2=n-1;i>=0;i--) 17 | { 18 | if(A[i]<=min) 19 | min=A[i]; 20 | else 21 | rd2=i; 22 | } 23 | if(!rd1) 24 | return 0; 25 | else 26 | return rd1-rd2+1; 27 | } 28 | }; 29 | int main() 30 | { 31 | int a[6]={1,4,6,5,9,10}; 32 | vector b(a,a+6); 33 | Subsequence c; 34 | int d=c.shortestSubsequence(b,6); 35 | cout< 2 | #include 3 | using namespace std; 4 | class Merge { 5 | public: 6 | int* mergeAB(int* A, int* B, int n, int m) 7 | { 8 | int i,j,NewLength=m+n; 9 | for(i=n-1,j=m-1;i>=0&&j>=0;NewLength--) 10 | { 11 | if(B[j]<=A[i]) 12 | A[NewLength-1]=A[i--]; 13 | else 14 | A[NewLength-1]=B[j--]; 15 | } 16 | while(j>=0) 17 | { 18 | A[NewLength-1]=B[j--]; 19 | NewLength--; 20 | } 21 | return A; 22 | } 23 | }; 24 | int main() 25 | { 26 | int a[10]={1,3,7,8}; 27 | int b[]={0,2,5,9}; 28 | Merge c; 29 | c.mergeAB(a,b,4,4); 30 | for(int i=0;i<8;i++) 31 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class Gap { 6 | public: 7 | int maxGap(vector A, int n) 8 | { 9 | vector> B(n+1); 10 | int max,min,i,j,maxGap; 11 | for(i=1,max=min=A[0];imax) 14 | max=A[i]; 15 | if(A[i]A[i]) 30 | B[j][0]=A[i]; 31 | if(B[j][1]=n+1) 46 | break; 47 | min=B[j][0]; 48 | if(min-max>maxGap) 49 | maxGap=min-max; 50 | } 51 | } 52 | return maxGap; 53 | } 54 | }; 55 | int main() 56 | { 57 | int a[5]={1,2,5,4,6}; 58 | vector arr(a,a+5); 59 | Gap b; 60 | int c=b.maxGap(arr,5); 61 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class Parenthesis { 6 | public: 7 | bool chkParenthesis(string A, int n) 8 | { 9 | int number=0,i; 10 | for(i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | class DistinctSubstring { 6 | public: 7 | int longestSubstring(string A, int n) 8 | { 9 | map map_table; 10 | int pre=0; 11 | int *dp=new int[n](); 12 | for(int i=0;i=(i-pre)) 17 | dp[i]=i-map_table[A[i]]; 18 | else 19 | dp[i]=pre+1; 20 | } 21 | else 22 | dp[i]=pre+1; 23 | pre=dp[i]; 24 | map_table[A[i]]=i; 25 | } 26 | int res=0; 27 | for(int i=1;ires) 29 | res=dp[i]; 30 | delete []dp; 31 | dp=NULL; 32 | return res; 33 | } 34 | }; 35 | int main() 36 | { 37 | string s("aabcb"); 38 | DistinctSubstring d; 39 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class Replacement { 6 | public: 7 | string replaceSpace(string iniString, int length) 8 | { 9 | int NumofBlank=0,i,j; 10 | for(i=0;i=0;) 16 | if(iniString[i]==' ') 17 | { 18 | res[j--]='0'; 19 | res[j--]='2'; 20 | res[j--]='%'; 21 | i--; 22 | } 23 | else 24 | res[j--]=iniString[i--]; 25 | return res; 26 | } 27 | }; 28 | int main() 29 | { 30 | string arr("a b c"); 31 | Replacement A; 32 | string res=A.replaceSpace(arr,5); 33 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class Transform { 6 | public: 7 | bool chkTransform(string A, int lena, string B, int lenb) 8 | { 9 | if(lena!=lenb) 10 | return false; 11 | int HashTable1[256]={0},HashTable2[256]={0},i; 12 | for(i=0;i=256) 21 | return true; 22 | else 23 | return false; 24 | } 25 | }; 26 | int main() 27 | { 28 | string a("abce"),b("rbca"); 29 | Transform A; 30 | bool c=A.chkTransform(a,4,b,4); 31 | cout< 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class TwoStacks { 7 | public: 8 | vector twoStacksSort(vector numbers) 9 | { 10 | stack sta; 11 | for(vector::reverse_iterator riter=numbers.rbegin();riter!=numbers.rend();riter++) 12 | sta.push(*riter); 13 | StackSort(sta); 14 | vector res; 15 | while(!sta.empty()) 16 | { 17 | res.push_back(sta.top()); 18 | sta.pop(); 19 | } 20 | return res; 21 | } 22 | void StackSort(stack &sta) 23 | { 24 | stack help; 25 | while(!sta.empty()) 26 | { 27 | int res=sta.top(); 28 | sta.pop(); 29 | if(help.empty()||res<=help.top()) 30 | help.push(res); 31 | else 32 | { 33 | while(!help.empty()&&res>help.top()) 34 | { 35 | sta.push(help.top()); 36 | help.pop(); 37 | } 38 | help.push(res); 39 | } 40 | } 41 | while(!help.empty()) 42 | { 43 | sta.push(help.top()); 44 | help.pop(); 45 | } 46 | } 47 | }; 48 | int main() 49 | { 50 | int a[5]={1,2,3,4,5}; 51 | TwoStacks A; 52 | vector arr(a,a+5),res; 53 | res=A.twoStacksSort(arr); 54 | for(vector::iterator iter=res.begin();iter!=res.end();iter++) 55 | cout<<*iter<<" "; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /ch4 队列和栈/双栈排序练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch4 队列和栈/双栈排序练习题/捕获.PNG -------------------------------------------------------------------------------- /ch4 队列和栈/双栈队列练习题/TwoStack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class TwoStack { 7 | public: 8 | stack stack_push,stack_pop; 9 | vector twoStack(vector ope, int n) 10 | { 11 | vector res; 12 | int i; 13 | for(i=0;i0) 16 | push(ope[i]); 17 | if(ope[i]==0) 18 | res.push_back(pop()); 19 | if(ope[i]<0) 20 | exit(-1); 21 | } 22 | return res; 23 | } 24 | void push(int value) 25 | { 26 | stack_push.push(value); 27 | } 28 | int pop() 29 | { 30 | if(stack_pop.empty()) 31 | while(!stack_push.empty()) 32 | { 33 | stack_pop.push(stack_push.top()); 34 | stack_push.pop(); 35 | } 36 | int res=stack_pop.top(); 37 | stack_pop.pop(); 38 | return res; 39 | } 40 | 41 | }; 42 | int main() 43 | { 44 | int a[6]={1,2,3,0,4,0}; 45 | vector arr(a,a+6); 46 | TwoStack A; 47 | vector res=A.twoStack(arr,6); 48 | for(vector::iterator iter=res.begin();iter!=res.end();iter++) 49 | cout<<*iter<<" "; 50 | return 0; 51 | } -------------------------------------------------------------------------------- /ch4 队列和栈/双栈队列练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch4 队列和栈/双栈队列练习题/捕获.PNG -------------------------------------------------------------------------------- /ch4 队列和栈/可查询最值的栈练习题/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | using namespace std; 5 | class Solution { 6 | public: 7 | stack soruce_data,min_data; 8 | void push(int value) 9 | { 10 | soruce_data.push(value); 11 | if(min_data.empty()||min_data.top()>=value) 12 | min_data.push(value); 13 | else 14 | min_data.push(min_data.top()); 15 | } 16 | void pop() 17 | { 18 | soruce_data.pop(); 19 | min_data.pop(); 20 | } 21 | int top() 22 | { 23 | return soruce_data.top(); 24 | } 25 | int min() 26 | { 27 | return min_data.top(); 28 | } 29 | }; 30 | int main() 31 | { 32 | Solution A; 33 | A.push(3); 34 | A.push(4); 35 | A.push(5); 36 | int c=A.min(); 37 | cout< 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class MaxTree { 7 | public: 8 | vector buildMaxTree(vector A, int n) 9 | { 10 | stack sta; 11 | vector left,res; 12 | int i; 13 | for(i=0;i st; 24 | vector right(n,0); 25 | for(i=n-1;i>=0;i--) 26 | { 27 | while(!st.empty()&&A[st.top()]<=A[i]) 28 | st.pop(); 29 | if(st.empty()) 30 | right[i]=-1; 31 | else 32 | right[i]=st.top(); 33 | st.push(i); 34 | } 35 | for(i=0;ileft[i]?right[i]:left[i]); 41 | else 42 | { 43 | if(A[right[i]]>A[left[i]]) 44 | res.push_back(left[i]); 45 | else 46 | res.push_back(right[i]); 47 | } 48 | } 49 | return res; 50 | } 51 | }; 52 | int main() 53 | { 54 | int a[4]={3,1,4,2}; 55 | MaxTree A; 56 | vector arr(a,a+4),res; 57 | res=A.buildMaxTree(arr,4); 58 | for(vector::iterator iter=res.begin();iter!=res.end();iter++) 59 | cout<<*iter<<" "; 60 | return 0; 61 | } -------------------------------------------------------------------------------- /ch4 队列和栈/数组变树练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch4 队列和栈/数组变树练习题/捕获.PNG -------------------------------------------------------------------------------- /ch4 队列和栈/栈的反转练习题/StackReverse.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch4 队列和栈/栈的反转练习题/StackReverse.cpp -------------------------------------------------------------------------------- /ch4 队列和栈/栈的反转练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch4 队列和栈/栈的反转练习题/捕获.PNG -------------------------------------------------------------------------------- /ch4 队列和栈/滑动窗口练习题/SlideWindow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class SlideWindow 7 | { 8 | public: 9 | vector slide(vector arr, int n, int w) 10 | { 11 | if(w==1) 12 | return arr; 13 | deque deq; 14 | vector res; 15 | int i; 16 | for(i=0;iarr[i]) 19 | deq.push_back(i); 20 | else 21 | { 22 | while(!deq.empty()&&arr[deq.back()]<=arr[i]) 23 | deq.pop_back(); 24 | deq.push_back(i); 25 | } 26 | while((i-deq.front())>=w) 27 | deq.pop_front(); 28 | if(i arr(a,a+8),res; 40 | res=A.slide(arr,8,3); 41 | for(vector::iterator iter=res.begin();iter!=res.end();iter++) 42 | cout<<*iter<<" "; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /ch4 队列和栈/滑动窗口练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch4 队列和栈/滑动窗口练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/单链表相交判断练习题/ChkIntersection.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/单链表相交判断练习题/ChkIntersection.cpp -------------------------------------------------------------------------------- /ch5 链表/单链表相交判断练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/单链表相交判断练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/复杂链表的复制练习题/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct RandomListNode { 7 | int label; 8 | struct RandomListNode *next, *random; 9 | RandomListNode(int x) : 10 | label(x), next(NULL), random(NULL) { 11 | } 12 | }; 13 | class Solution { 14 | public: 15 | RandomListNode* Clone(RandomListNode* pHead) 16 | { 17 | if(!pHead) 18 | return pHead; 19 | RandomListNode *p=pHead,*temp=NULL,*New=NULL; 20 | while(p) 21 | { 22 | temp=(*p).next; 23 | New=new RandomListNode((*p).label); 24 | (*New).next=(*p).next; 25 | (*p).next=New; 26 | p=temp; 27 | } 28 | p=pHead,New=(*p).next; 29 | while(p) 30 | { 31 | if((*p).random) 32 | (*New).random=(*(*p).random).next; 33 | p=(*New).next; 34 | if(p) 35 | New=(*p).next; 36 | } 37 | p=pHead,New=(*p).next; 38 | RandomListNode* NHead=New; 39 | while((*New).next) 40 | { 41 | temp=(*New).next; 42 | (*p).next=(*New).next; 43 | (*New).next=(*temp).next; 44 | p=temp; 45 | New=(*p).next; 46 | } 47 | (*p).next=NULL; 48 | return NHead; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /ch5 链表/复杂链表的复制练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/复杂链表的复制练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/打印两个链表的公共值练习题/Common.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class Common { 12 | public: 13 | vector findCommonParts(ListNode* headA, ListNode* headB) 14 | { 15 | vector res; 16 | ListNode *p=headA,*q=headB; 17 | while(p&&q) 18 | { 19 | if((*p).val==(*q).val) 20 | { 21 | res.push_back((*p).val); 22 | p=(*p).next; 23 | q=(*q).next; 24 | } 25 | else if((*p).val<(*q).val) 26 | p=(*p).next; 27 | else 28 | q=(*q).next; 29 | } 30 | return res; 31 | } 32 | }; 33 | class CreatListNode 34 | { 35 | public: 36 | ListNode* Creat(vector A) 37 | { 38 | if(A.empty()) 39 | return NULL; 40 | ListNode *Head=new ListNode(A[0]),*p=Head; 41 | for(vector::iterator iter=A.begin()+1;iter!=A.end();iter++) 42 | { 43 | (*p).next=new ListNode(*iter); 44 | p=(*p).next; 45 | } 46 | return Head; 47 | } 48 | void Print(ListNode* Head) 49 | { 50 | ListNode *p=Head; 51 | while(p) 52 | { 53 | cout<<(*p).val<<" "; 54 | p=(*p).next; 55 | } 56 | } 57 | }; 58 | int main() 59 | { 60 | int a[7]={1,2,3,4,5,6,7},b[5]={2,4,6,8,10}; 61 | vector arr(a,a+7),brr(b,b+5); 62 | CreatListNode C; 63 | ListNode *HeadA=C.Creat(arr); 64 | ListNode *HeadB=C.Creat(brr); 65 | Common D; 66 | vector res=D.findCommonParts(HeadA,HeadB); 67 | for(vector::iterator iter=res.begin();iter!=res.end();iter++) 68 | cout<<*iter<<" "; 69 | return 0; 70 | } -------------------------------------------------------------------------------- /ch5 链表/打印两个链表的公共值练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/打印两个链表的公共值练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/无环单链表判相交练习题/CheckIntersect.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class CheckIntersect { 12 | public: 13 | bool chkIntersect(ListNode* headA, ListNode* headB) 14 | { 15 | if(!headA||!headB) 16 | return false; 17 | int NumofA=0,NumofB=0,count=0; 18 | ListNode *p=headA; 19 | while(p) 20 | { 21 | NumofA++; 22 | p=(*p).next; 23 | } 24 | p=headB; 25 | while(p) 26 | { 27 | NumofB++; 28 | p=(*p).next; 29 | } 30 | ListNode *L=NULL,*S=NULL; 31 | if(NumofA>NumofB) 32 | L=headA,S=headB; 33 | else 34 | L=headB,S=headA; 35 | count=(NumofA>NumofB)?(NumofA-NumofB):(NumofB-NumofA); 36 | while(count--) 37 | L=(*L).next; 38 | while(L&&S) 39 | { 40 | if(L==S) 41 | return true; 42 | L=(*L).next; 43 | S=(*S).next; 44 | } 45 | return false; 46 | } 47 | }; 48 | int main() 49 | { 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /ch5 链表/无环单链表判相交练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/无环单链表判相交练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/有环单链表相交判断练习题/ChkIntersection.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/有环单链表相交判断练习题/ChkIntersection.cpp -------------------------------------------------------------------------------- /ch5 链表/有环单链表相交判断练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/有环单链表相交判断练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/环形链表插值练习题/InsertValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class InsertValue { 12 | public: 13 | ListNode* insert(vector A, vector nxt, int val) 14 | { 15 | if(A.empty()) 16 | { 17 | ListNode *Head=new ListNode(val); 18 | //(*Head).next=Head;//1 19 | return Head; 20 | } 21 | ListNode *Head=new ListNode(A[0]),*p=Head; 22 | for(vector::iterator iter=nxt.begin();iter!=nxt.end();iter++) 23 | { 24 | if(*iter==0) 25 | { 26 | (*p).next=Head; 27 | break; 28 | } 29 | (*p).next=new ListNode(A[*iter]); 30 | p=(*p).next; 31 | } 32 | ListNode *pre=Head,*now=(*Head).next; 33 | while(now!=Head) 34 | { 35 | if((*pre).val<=val&&(*now).val>=val) 36 | { 37 | ListNode *p=new ListNode(val); 38 | (*p).next=now; 39 | (*pre).next=p; 40 | break; 41 | } 42 | else 43 | { 44 | pre=now; 45 | now=(*now).next; 46 | } 47 | } 48 | if(now==Head) 49 | { 50 | ListNode *p=new ListNode(val); 51 | (*p).next=now; 52 | (*pre).next=p; 53 | if(val<(*Head).val) 54 | Head=p; 55 | } 56 | pre=Head,p=(*Head).next;//2 57 | while(p!=Head) 58 | { 59 | pre=p; 60 | p=(*p).next; 61 | } 62 | (*pre).next=NULL;//2 63 | return Head; 64 | } 65 | }; 66 | 67 | int main() 68 | { 69 | int a[7]={3,3,7,7,8,12,13},nxt[7]={1,2,3,4,5,6,0}; 70 | vector arr(a,a+7),next(nxt,nxt+7); 71 | InsertValue s; 72 | ListNode* Head=s.insert(arr,next,11); 73 | ListNode *p=(*Head).next; 74 | cout<<(*Head).val<<" "; 75 | while(p!=NULL) 76 | { 77 | cout<<(*p).val<<" "; 78 | p=(*p).next; 79 | } 80 | return 0; 81 | } -------------------------------------------------------------------------------- /ch5 链表/环形链表插值练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/环形链表插值练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/访问单个节点的删除练习题/Remove.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class Remove { 12 | public: 13 | bool removeNode(ListNode* pNode) 14 | { 15 | if((*pNode).next==NULL) 16 | return false; 17 | ListNode *p=(*pNode).next,*pre=pNode; 18 | while((*p).next!=NULL) 19 | { 20 | (*pre).val=(*p).val; 21 | pre=p; 22 | p=(*p).next; 23 | } 24 | (*pre).val=(*p).val; 25 | (*pre).next=NULL; 26 | delete p; 27 | p=NULL; 28 | return true; 29 | } 30 | }; 31 | int main() 32 | { 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /ch5 链表/访问单个节点的删除练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/访问单个节点的删除练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/链表判环练习题/ChkLoop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class ChkLoop { 12 | public: 13 | int chkLoop(ListNode* head, int adjust) 14 | { 15 | if(!head) 16 | return -1; 17 | ListNode *slow=head,*fast=head; 18 | while(((*fast).next&&(*(*fast).next).next)) 19 | { 20 | slow=(*slow).next; 21 | fast=(*(*fast).next).next; 22 | if(slow==fast) 23 | break; 24 | } 25 | if(!(*fast).next||!(*(*fast).next).next) 26 | return -1; 27 | slow=head; 28 | while(slow!=fast) 29 | { 30 | slow=(*slow).next; 31 | fast=(*fast).next; 32 | } 33 | return (*fast).val; 34 | } 35 | }; 36 | int main() 37 | { 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /ch5 链表/链表判环练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表判环练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/链表指定值清除练习题/ClearValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class ClearValue { 12 | public: 13 | ListNode* clear(ListNode* head, int val) 14 | { 15 | if(!head) 16 | return head; 17 | ListNode *Head=NULL,*Tail=NULL,*p=head,*temp=NULL; 18 | while(p) 19 | { 20 | temp=(*p).next; 21 | if((*p).val==val) 22 | delete p; 23 | else 24 | { 25 | if(!Head) 26 | Head=p; 27 | else 28 | (*Tail).next=p; 29 | Tail=p; 30 | (*Tail).next=NULL; 31 | } 32 | p=temp; 33 | } 34 | return Head; 35 | } 36 | }; 37 | class CreatListNode 38 | { 39 | public: 40 | ListNode* Creat(vector A) 41 | { 42 | if(A.empty()) 43 | return NULL; 44 | ListNode *Head=new ListNode(A[0]),*p=Head; 45 | for(vector::iterator iter=A.begin()+1;iter!=A.end();iter++) 46 | { 47 | (*p).next=new ListNode(*iter); 48 | p=(*p).next; 49 | } 50 | return Head; 51 | } 52 | void Print(ListNode* Head) 53 | { 54 | ListNode *p=Head; 55 | while(p) 56 | { 57 | cout<<(*p).val<<" "; 58 | p=(*p).next; 59 | } 60 | } 61 | }; 62 | int main() 63 | { 64 | int a[7]={1,2,3,4,3,2,1}; 65 | vector arr(a,a+7); 66 | CreatListNode C; 67 | ListNode *HeadA=C.Creat(arr); 68 | ClearValue D; 69 | ListNode *head=D.clear(HeadA,2); 70 | C.Print(head); 71 | return 0; 72 | } -------------------------------------------------------------------------------- /ch5 链表/链表指定值清除练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表指定值清除练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/链表的k逆序练习题/KInverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class KInverse { 12 | public: 13 | ListNode* inverse(ListNode* head, int k) 14 | { 15 | if(!head||k<2) 16 | return head; 17 | ListNode *p=head,*q=head,*LastTail=NULL; 18 | int i=1,count=0; 19 | while(q) 20 | { 21 | if(i==k) 22 | { 23 | if(!count) 24 | head=q; 25 | else 26 | (*LastTail).next=q; 27 | ListNode *temp1=(*q).next,*temp2=NULL; 28 | while(q!=p) 29 | { 30 | temp2=(*p).next; 31 | if((*q).next==temp1) 32 | LastTail=p; 33 | (*p).next=(*q).next; 34 | (*q).next=p; 35 | p=temp2; 36 | } 37 | p=q=temp1; 38 | i=1; 39 | count++; 40 | } 41 | if(q) 42 | q=(*q).next; 43 | i++; 44 | } 45 | return head; 46 | } 47 | }; 48 | class CreatListNode 49 | { 50 | public: 51 | ListNode* Creat(vector A) 52 | { 53 | if(A.empty()) 54 | return NULL; 55 | ListNode *Head=new ListNode(A[0]),*p=Head; 56 | for(vector::iterator iter=A.begin()+1;iter!=A.end();iter++) 57 | { 58 | (*p).next=new ListNode(*iter); 59 | p=(*p).next; 60 | } 61 | return Head; 62 | } 63 | void Print(ListNode* Head) 64 | { 65 | ListNode *p=Head; 66 | while(p) 67 | { 68 | cout<<(*p).val<<" "; 69 | p=(*p).next; 70 | } 71 | } 72 | }; 73 | int main() 74 | { 75 | int a[8]={1,2,3,4,5,6,7,8}; 76 | vector arr(a,a+8); 77 | CreatListNode C; 78 | ListNode *HeadA=C.Creat(arr); 79 | KInverse D; 80 | ListNode *head=D.inverse(HeadA,3); 81 | C.Print(head); 82 | return 0; 83 | } -------------------------------------------------------------------------------- /ch5 链表/链表的k逆序练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表的k逆序练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/链表的分化练习题/Divide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | struct ListNode { 7 | int val; 8 | struct ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class Divide { 12 | public: 13 | ListNode* listDivide(ListNode* head, int val) 14 | { 15 | ListNode *smallh=NULL,*smallt=NULL,*bigh=NULL,*bigt=NULL,*p=head,*q=head; 16 | while(p) 17 | { 18 | q=p; 19 | if((*p).val<=val) 20 | { 21 | if(!smallh) 22 | smallh=smallt=p; 23 | else 24 | { 25 | (*smallt).next=p; 26 | smallt=p; 27 | } 28 | } 29 | else 30 | { 31 | if(!bigh) 32 | bigh=bigt=p; 33 | else 34 | { 35 | (*bigt).next=p; 36 | bigt=p; 37 | } 38 | } 39 | p=(*p).next; 40 | (*q).next=NULL; 41 | } 42 | if(smallh) 43 | { 44 | head=smallh; 45 | if(bigh) 46 | (*smallt).next=bigh; 47 | } 48 | else 49 | if(bigh) 50 | head=bigh; 51 | return head; 52 | } 53 | }; 54 | class CreatListNode 55 | { 56 | public: 57 | ListNode* Creat(vector A) 58 | { 59 | if(A.empty()) 60 | return NULL; 61 | ListNode *Head=new ListNode(A[0]),*p=Head; 62 | for(vector::iterator iter=A.begin()+1;iter!=A.end();iter++) 63 | { 64 | (*p).next=new ListNode(*iter); 65 | p=(*p).next; 66 | } 67 | return Head; 68 | } 69 | void Print(ListNode* Head) 70 | { 71 | ListNode *p=Head; 72 | while(p) 73 | { 74 | cout<<(*p).val<<" "; 75 | p=(*p).next; 76 | } 77 | } 78 | }; 79 | int main() 80 | { 81 | int a[5]={1620,1134,861,563,1}; 82 | vector arr(a,a+5); 83 | CreatListNode C; 84 | ListNode *Head=C.Creat(arr); 85 | Divide D; 86 | Head=D.listDivide(Head,1134); 87 | C.Print(Head); 88 | return 0; 89 | } -------------------------------------------------------------------------------- /ch5 链表/链表的分化练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表的分化练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/链表的回文结构练习题/Palindrome.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表的回文结构练习题/Palindrome.cpp -------------------------------------------------------------------------------- /ch5 链表/链表的回文结构练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表的回文结构练习题/捕获.PNG -------------------------------------------------------------------------------- /ch5 链表/链表相交小结.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch5 链表/链表相交小结.txt -------------------------------------------------------------------------------- /ch6 二分搜索/元素最左出现练习题/LeftMostAppearance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class LeftMostAppearance 7 | { 8 | public: 9 | int findPos(vector arr, int n, int num) 10 | { 11 | if(!n) 12 | return -1; 13 | int low=0,high=n-1,mid; 14 | int res=-1; 15 | while(low<=high) 16 | { 17 | mid=low+(high-low)/2; 18 | if(arr[mid]num) 21 | high=mid-1; 22 | else 23 | { 24 | res=mid; 25 | high=mid-1; 26 | } 27 | } 28 | return res; 29 | } 30 | }; 31 | int main() 32 | { 33 | int a[5]={1,2,3,3,4}; 34 | vector arr(a,a+5); 35 | LeftMostAppearance s; 36 | cout< 2 | #include 3 | #include//pop,top,push 4 | #include 5 | #include 6 | using namespace std; 7 | struct TreeNode { 8 | int val; 9 | struct TreeNode *left; 10 | struct TreeNode *right; 11 | TreeNode(int x) : 12 | val(x), left(NULL), right(NULL) { 13 | } 14 | }; 15 | class CountNodes { 16 | public: 17 | int count(TreeNode* root) 18 | { 19 | if(!root) 20 | return 0; 21 | int Lengthofleft=DepthofCom_Bitree((*root).left); 22 | int Lengthofright=DepthofCom_Bitree((*root).right); 23 | if(Lengthofleft==Lengthofright) 24 | return pow(2.0,Lengthofleft)+count((*root).right); 25 | else 26 | return pow(2.0,Lengthofright)+count((*root).left); 27 | } 28 | int DepthofCom_Bitree(TreeNode *root) 29 | { 30 | if(!root) 31 | return 0; 32 | TreeNode *p=root; 33 | int len=0; 34 | while(p) 35 | { 36 | len++; 37 | p=(*p).left; 38 | } 39 | return len; 40 | } 41 | }; 42 | int main() 43 | { 44 | return 0; 45 | } -------------------------------------------------------------------------------- /ch6 二分搜索/完全二叉树计数练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch6 二分搜索/完全二叉树计数练习题/捕获.PNG -------------------------------------------------------------------------------- /ch6 二分搜索/局部最小值位置练习题/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class Solution 7 | { 8 | public: 9 | int getLessIndex(vector arr) 10 | { 11 | if(arr.empty()) 12 | return -1; 13 | if(arr.size()==1||arr[0]arr[mid-1]) 22 | high=mid-1; 23 | else if(arr[mid]>arr[mid+1]) 24 | low=mid+1; 25 | else 26 | return mid; 27 | } 28 | return -1; 29 | } 30 | }; 31 | int main() 32 | { 33 | int a[30]={10,5,10,5,0,1,2,4,7,3,2,9,5,4,6,5,10,6,7,10,9,4,3,7,2,9,5,4,6,10}; 34 | vector arr(a,a+30); 35 | Solution s; 36 | cout< 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class MinValue { 7 | public: 8 | int getMin(vector arr, int n) 9 | { 10 | if(arr.empty()) 11 | exit(-1); 12 | if(n==1||arr[0]arr[mid]) 19 | high=mid; 20 | else if(arr[mid]>arr[high]) 21 | low=mid+1; 22 | else 23 | break; 24 | } 25 | if(low==high) 26 | return arr[low]; 27 | int min=arr[low]; 28 | while(low<=high) 29 | { 30 | if(arr[low] arr(a,a+5); 41 | MinValue s; 42 | cout< 2 | #include 3 | #include//pop,top,push 4 | #include 5 | #include 6 | using namespace std; 7 | class QuickPower 8 | { 9 | public: 10 | int getPower(int k, int N) 11 | //(http://www.cnblogs.com/Knuth/archive/2009/09/04/1559949.html) 12 | { 13 | if(k==0) 14 | return 0; 15 | if(N==0) 16 | return 1; 17 | if(k>1000000007) 18 | k=k%1000000007; 19 | vector arr; 20 | vector bit; 21 | long long m=N,temp=k,res; 22 | while(m) 23 | { 24 | arr.push_back(temp); 25 | temp*=temp; 26 | if(temp>1000000007) 27 | temp=temp%1000000007; 28 | if(m%2) 29 | bit.push_back(1); 30 | else 31 | bit.push_back(0); 32 | m=m/2; 33 | } 34 | for(int i=0,res=1;i1000000007) 39 | res=res%1000000007; 40 | } 41 | return res%(1000000007); 42 | } 43 | }; 44 | int main() 45 | { 46 | QuickPower s; 47 | cout< 2 | #include 3 | #include//pop,top,push 4 | #include 5 | using namespace std; 6 | class Find { 7 | public: 8 | int findPos(vector arr, int n) 9 | { 10 | if(arr.empty()||arr[0]>n-1||arr[n-1]<0) 11 | return -1; 12 | int res=-1; 13 | int low=0,high=n-1,mid; 14 | while(low<=high) 15 | { 16 | mid=low+(high-low)/2; 17 | if(arr[mid]mid) 20 | high=mid-1; 21 | else 22 | { 23 | res=mid; 24 | high=mid-1; 25 | } 26 | } 27 | return res; 28 | } 29 | }; 30 | int main() 31 | { 32 | int a[5]={-1,0,2,3}; 33 | vector arr(a,a+4); 34 | Find s; 35 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | struct TreeNode { 8 | int val; 9 | struct TreeNode *left; 10 | struct TreeNode *right; 11 | TreeNode(int x) : 12 | val(x), left(NULL), right(NULL) { 13 | } 14 | }; 15 | class FindErrorNode { 16 | public: 17 | vector findError(TreeNode* root) 18 | { 19 | vector res(2,0); 20 | vector >t; 21 | int last=INT_MIN,now=0; 22 | stack sta; 23 | TreeNode *p=root,*temp=NULL; 24 | while(!sta.empty()||p) 25 | { 26 | while(p) 27 | { 28 | sta.push(p); 29 | p=(*p).left; 30 | } 31 | temp=sta.top(); 32 | sta.pop(); 33 | now=(*temp).val; 34 | if(last>now) 35 | { 36 | res[1]=last; 37 | res[0]=now; 38 | t.push_back(res); 39 | } 40 | last=now; 41 | p=(*temp).right; 42 | } 43 | if(t.size()!=1) 44 | { 45 | res[1]=t[0][1]; 46 | res[0]=t[1][0]; 47 | } 48 | return res; 49 | } 50 | }; 51 | 52 | 53 | int main() 54 | { 55 | return 0; 56 | } -------------------------------------------------------------------------------- /ch7 二叉树/寻找错误结点练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/寻找错误结点练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/平衡二叉树判断练习题/CheckBalance.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/平衡二叉树判断练习题/CheckBalance.cpp -------------------------------------------------------------------------------- /ch7 二叉树/平衡二叉树判断练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/平衡二叉树判断练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/折纸练习题/FoldPaper.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/折纸练习题/FoldPaper.cpp -------------------------------------------------------------------------------- /ch7 二叉树/折纸练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/折纸练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/最大二叉搜索子树练习题/MaxSubtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | struct TreeNode { 8 | int val; 9 | struct TreeNode *left; 10 | struct TreeNode *right; 11 | TreeNode(int x) : 12 | val(x), left(NULL), right(NULL) { 13 | } 14 | }; 15 | class MaxSubtree 16 | { 17 | public: 18 | TreeNode* getMax(TreeNode* root) 19 | { 20 | int Num=0,Min,Max; 21 | TreeNode *temp=NULL; 22 | PostOrder_GetMax(root,Num,Min,Max,temp); 23 | return temp; 24 | } 25 | void PostOrder_GetMax(TreeNode* root,int &NumofNode,int &MinValue,int &MaxValue,TreeNode* &pRoot) 26 | { 27 | if(!root) 28 | { 29 | pRoot=NULL; 30 | return; 31 | } 32 | int lNumofNode,lMin,lMax,rNumofNode,rMin,rMax; 33 | TreeNode *lpRoot=NULL,*rpRoot=NULL; 34 | lMin=lMax=rMin=rMax=(*root).val; 35 | lNumofNode=rNumofNode=0; 36 | PostOrder_GetMax((*root).left,lNumofNode,lMin,lMax,lpRoot); 37 | PostOrder_GetMax((*root).right,rNumofNode,rMin,rMax,rpRoot); 38 | if(lpRoot==(*root).left&&rpRoot==(*root).right&&(*root).val>=lMax&&(*root).val<=rMin) 39 | { 40 | pRoot=root; 41 | NumofNode=lNumofNode+rNumofNode+1; 42 | MinValue=lMin; 43 | MaxValue=rMax; 44 | return; 45 | } 46 | else 47 | { 48 | if(lNumofNode>rNumofNode||((lNumofNode==rNumofNode)&&(*lpRoot).val>(*rpRoot).val)) 49 | { 50 | pRoot=lpRoot; 51 | NumofNode=lNumofNode; 52 | MinValue=lMin; 53 | MaxValue=lMax; 54 | return; 55 | } 56 | pRoot=rpRoot; 57 | NumofNode=rNumofNode; 58 | MinValue=rMin; 59 | MaxValue=rMax; 60 | } 61 | return; 62 | } 63 | }; 64 | 65 | int main() 66 | { 67 | return 0; 68 | } -------------------------------------------------------------------------------- /ch7 二叉树/最大二叉搜索子树练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/最大二叉搜索子树练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/树上最远距离练习题/LongestDistance.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/树上最远距离练习题/LongestDistance.cpp -------------------------------------------------------------------------------- /ch7 二叉树/树上最远距离练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/树上最远距离练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/递归二叉树的序列打印练习题/TreeToSequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/递归二叉树的序列打印练习题/TreeToSequence.cpp -------------------------------------------------------------------------------- /ch7 二叉树/递归二叉树的序列打印练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/递归二叉树的序列打印练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/非递归二叉树的序列打印练习题/TreeToSequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/非递归二叉树的序列打印练习题/TreeToSequence.cpp -------------------------------------------------------------------------------- /ch7 二叉树/非递归二叉树的序列打印练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/非递归二叉树的序列打印练习题/捕获.PNG -------------------------------------------------------------------------------- /ch7 二叉树/非递归二叉树的序列打印练习题/非递归中序.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/非递归二叉树的序列打印练习题/非递归中序.PNG -------------------------------------------------------------------------------- /ch7 二叉树/非递归二叉树的序列打印练习题/非递归先序.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/非递归二叉树的序列打印练习题/非递归先序.PNG -------------------------------------------------------------------------------- /ch7 二叉树/非递归二叉树的序列打印练习题/非递归后序1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/非递归二叉树的序列打印练习题/非递归后序1.PNG -------------------------------------------------------------------------------- /ch7 二叉树/非递归二叉树的序列打印练习题/非递归后序2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch7 二叉树/非递归二叉树的序列打印练习题/非递归后序2.PNG -------------------------------------------------------------------------------- /ch8 位运算/交换练习题/Swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Swap { 5 | public: 6 | vector getSwap(vector num) 7 | { 8 | num[0]=num[0]^num[1]; 9 | num[1]=num[0]^num[1]; 10 | num[0]=num[0]^num[1]; 11 | return num; 12 | } 13 | }; -------------------------------------------------------------------------------- /ch8 位运算/交换练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch8 位运算/交换练习题/捕获.PNG -------------------------------------------------------------------------------- /ch8 位运算/寻找奇数出现II练习题/OddAppearanc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class OddAppearance { 5 | public: 6 | vector findOdds(vector arr, int n) 7 | { 8 | vector res; 9 | int check1=0; 10 | for(int i=0;i>=1; 17 | } 18 | int help=pow(2.0,k),check2=0; 19 | for(int i=0;icheck2?check1:check2); 25 | return res; 26 | } 27 | }; -------------------------------------------------------------------------------- /ch8 位运算/寻找奇数出现II练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch8 位运算/寻找奇数出现II练习题/捕获.PNG -------------------------------------------------------------------------------- /ch8 位运算/寻找奇数出现练习题/OddAppearanc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class OddAppearance { 5 | public: 6 | int findOdd(vector A, int n) 7 | { 8 | int temp=0; 9 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | class TreeCount 5 | { 6 | public: 7 | int countWays(int n) 8 | { 9 | return zuhe(2*n,n)/(n+1); 10 | } 11 | int zuhe(int m,int n) 12 | //n<=m 13 | { 14 | int A=1,B=1,i=0; 15 | while(i 2 | #include 3 | using namespace std; 4 | class Distribution { 5 | public: 6 | int getWays(int n, int m) 7 | { 8 | return zuhe(n-1,m-1); 9 | } 10 | int zuhe(int m,int n) 11 | //n<=m 12 | { 13 | int A=1,B=1,i=0; 14 | while(i 2 | #include 3 | using namespace std; 4 | class LonelyA { 5 | public: 6 | int getWays(int n, int A, int b, int c) 7 | { 8 | int s1=pailie(n),s2=pailie(n-1),s3=pailie(n-2); 9 | return (s1-4*s2+2*s3); 10 | 11 | } 12 | int pailie(int m) 13 | { 14 | int res=1; 15 | while(m) 16 | { 17 | res*=m; 18 | m--; 19 | } 20 | return res; 21 | } 22 | }; -------------------------------------------------------------------------------- /ch9 排列与组合/孤傲的A练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch9 排列与组合/孤傲的A练习题/捕获.PNG -------------------------------------------------------------------------------- /ch9 排列与组合/括号序列练习题/Parenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Parenthesis { 5 | public: 6 | int countLegalWays(int n) 7 | { 8 | return zuhe(2*n,n)/(n+1); 9 | } 10 | int zuhe(int m,int n) 11 | //n<=m 12 | { 13 | int A=1,B=1,i=0; 14 | while(i 2 | #include 3 | using namespace std; 4 | class BuyTickets { 5 | public: 6 | int countWays(int n) 7 | { 8 | return zuhe(2*n,n)/(n+1); 9 | } 10 | int zuhe(int m,int n) 11 | //n<=m 12 | { 13 | int A=1,B=1,i=0; 14 | while(i 2 | #include 3 | using namespace std; 4 | class Robot { 5 | public: 6 | int countWays(int x, int y) 7 | { 8 | return zuhe(x+y-2,x-1); 9 | 10 | } 11 | int zuhe(int m,int n) 12 | { 13 | int A=1,B=1,i=0; 14 | while(i 2 | #include 3 | using namespace std; 4 | class StandInLine { 5 | public: 6 | vector getWays(int n, int a, int b) 7 | { 8 | vector res; 9 | res.push_back(pailie(n)/2); 10 | res.push_back(pailie(n-1)); 11 | return res; 12 | 13 | } 14 | int pailie(int m) 15 | { 16 | int res=1; 17 | while(m) 18 | { 19 | res*=m; 20 | m--; 21 | } 22 | return res; 23 | } 24 | }; -------------------------------------------------------------------------------- /ch9 排列与组合/站队问题练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch9 排列与组合/站队问题练习题/捕获.PNG -------------------------------------------------------------------------------- /ch9 排列与组合/进出栈练习题/Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Stack { 5 | public: 6 | int countWays(int n) 7 | { 8 | return zuhe(2*n,n)/(n+1); 9 | } 10 | int zuhe(int m,int n) 11 | //n<=m 12 | { 13 | int A=1,B=1,i=0; 14 | while(i 2 | #include 3 | using namespace std; 4 | #define Mod 1000000007 5 | class CombineByMistake { 6 | public: 7 | int countWays(int n) 8 | { 9 | if(n==0||n==1) 10 | return 0; 11 | if(n==2) 12 | return 1; 13 | int pre=0,last=1,i,temp; 14 | for(i=3,temp=0;i<=n;i++) 15 | { 16 | temp=(long long)(i-1)*(long long)(last+pre)%Mod; 17 | pre=last; 18 | last=temp; 19 | } 20 | return last; 21 | } 22 | }; -------------------------------------------------------------------------------- /ch9 排列与组合/错装信封练习题/捕获.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyitao/-BAT-/a827e4d1a6c27ef1f5c72c0639c15ebf5b36f6b5/ch9 排列与组合/错装信封练习题/捕获.PNG -------------------------------------------------------------------------------- /ch9 排列与组合/高矮排列练习题/HighAndShort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class HighAndShort 5 | { 6 | public: 7 | int countWays(int n) 8 | { 9 | return zuhe(n,n/2)/(n/2+1); 10 | } 11 | int zuhe(int m,int n) 12 | //n<=m 13 | { 14 | int A=1,B=1,i=0; 15 | while(i