Sword Finger Offer Daquan
Write in front
Generally speaking, the offer is divided into several brushes
- Quickly brush, do not understand the direct problem solution, get a positive solution.
- Silently brush, read the solution again, write silently, and consolidate
- Daily brush, three times a day to save my body, the next day can brush the square to go further
- After completing all the topics, you should review it from time to time, "Thinking but not learning, you will die." You must keep moving, and the speed will be faster."The question is changed, and the meaning must be revealed."
In the end, we can do more and more difficult questions, but it is always the same. The simplest offer is our compulsory internal skills!
Problem solution table
Sword refers to the offer question number | Solution address | all in all |
---|---|---|
JT1 | Lookup in a two-dimensional array | Elegant violence is always so charming |
JT2 | Replace spaces | The bells and whistles will eventually mispay the time |
JT3 | Print linked list from end to beginning | In order to save space I choose to rotate |
JT4 | Rebuild Binary Tree | A broken mirror is difficult to reround, but a binary tree can |
JT5 | Implement the queue with two stacks | Use magic to defeat magic |
JT6 | Rotate the smallest number of the array | Who is violent and who is a fool |
JT7 | Fibonacci sequence | Memoized search is important |
JT8 | Step up | I can tell at a glance that it's not a step |
JT9 | Abnormal jump | Next to the n-level steps of the ribs, it's really awesome |
JT10 | Rectangular coverage | Everything has a cause, but the answer is hidden |
JT11 | Number of 1s in binary | n&(n-1) You have to know the meaning of の0.0! |
JT12 | Integer power | Let's talk about the [Fast Power] algorithm! |
JT13 | Adjust the sequence of the array so that the odd numbers are before the even numbers | Too simple |
JT14 | The kth node from the bottom in the linked list | Shock! ! ! Solve problems easily with the relative movement of two pointers |
JT15 | Reverse linked list | No step without silicon accumulation |
JT16 | Merge two sorted linked lists | Do not forget the original intention, always have to |
JT17 | Tree substructure | Tree comparisons are all recursive |
JT18 | Mirror of Binary Tree | The mirroring of the tree is also recursive |
JT19 | Print matrix clockwise | Square jungle |
JT20 | Stack containing min function | Wouldn't it be enough to save the minimum value? |
JT21 | Stack push, pop sequence | Wait till you show up |
JT22 | Print binary tree from top to bottom | Use the queue to protect everyone on the tree! |
JT23 | Post-order traversal sequence of binary search tree | Bitter before sweet |
JT24 | A path whose sum is a certain value in a binary tree | Promise me, don’t give up, just run the tree |
JT25 | Replication of complex linked lists | Step by step is too difficult, map knows me best |
JT26 | Binary search tree and doubly linked list | After pondering for a few days, it is quite abstract |
JT27 | String arrangement | Drunkard doesn’t mean wine |
JT28 | Numbers with more than half of the number of occurrences in the array | Use the hash table to support the edge! |
JT29 | The smallest number of K | Pretty boy and pretty girl, come in and learn quickly! |
JT30 | Maximum sum of consecutive subarrays | Dynamic planning [the most detailed illustration of the entire network] |
JT31 | The number of occurrences of 1 in an integer from 1 to n | Everything is in order, especially numbers |
JT32 | Arrange the array into the smallest number | sort() can write sorting rules! |
JT33 | Ugly number | The guest officer came in to draw a lucky number! |
JT34 | ||
JT35 | ||
JT36 | ||
JT37 | ||
JT38 | ||
JT39 | ||
JT40 | ||
JT41 | ||
JT42 | ||
JT43 | ||
JT44 | ||
JT45 | ||
JT46 | ||
JT47 | ||
JT48 | ||
JT49 | ||
JT50 | ||
JT51 | ||
JT52 | ||
JT53 | ||
JT54 | ||
JT55 | ||
JT56 | ||
JT57 | ||
JT58 | ||
JT59 | ||
JT60 | ||
JT61 | ||
JT62 | ||
JT63 | ||
JT64 | ||
JT65 | ||
JT66 | ||
JT67 |
Silent Writing Brush Encounter Mistakes
JT4—Rebuild Binary Tree
node->left=reBuiled(pre,vin,pre_left+1,pre_left+index-vin_left,vin_left, index-1);
node->right=reBuiled(pre,vin,pre_left+index-vin_left+1,pre_right,index+1,vin_right);
node->right的pre_left+index-vin_left +1
It seems to be fine, but because pre_left+index-vin_left should be the last left node of the follow node, but it is precisely becauseNo +1 will cause insufficient memory, Or more rigorous, +1 is perfect!
JT12—The integer power of the value
In the process of using fast exponents, the = dropped when the exponent is shifted, causing the program to fail to end and the program to time out
exponent>>=1;//正确的移位操作
exponent>>1;//由于掉了=,导致exponent一直没有改变,进入了死循环
JT14—the kth node from the bottom in the linked list
The double pointer forgot to judge the out-of-bounds return of the fast pointer, resulting in a segfault.
while(k){
if(!fast) return nullptr;//如果出现了k大于链表成都的情况,这个判断就可以输出空
k--;
fast=fast->next;
}
JT16—Merge two sorted linked lists
1. The return value is head->next; if the return is head, there will be an extra 0 at the top, because when applying for space, the assignment of the header is 0.
2. The condition of while is until one of them After the linked list is finished, the rest of the other linked list can be placed directly after p->next
while(pHead1&&pHead2)//正确的写法
while(!pHead1||!pHead2)//其中一个为零才开始循环 这是不对了,注意while,我们应该要的是,都不为零才开始循环
JT26—Binary search tree and doubly linked list
JT35—Reversed pairs in an array
Why is the subsequence in order after calling recursion several times?
Topic classification table
数组
剑指offer题号 | 题解地址 |
---|---|
JT1 | 二维数组中的查找 |
JT7 | Fibonacci数列 |
JT13 | 调整数组循序使奇数位于偶数前面 |
JT19 | 顺时针打印矩阵 |
JT28 | 数组种出现次数超过一半的数字 |
JT32 | 把数组排成最小的数 |
字符串
剑指offer题号 | 题解地址 |
---|---|
JT2 | 替换空格 |
JT27 | 字符串的排列 |
链表
剑指offer题号 | 题解地址 |
---|---|
JT3 | 从尾到头打印链表 |
JT14 | 链表中倒数第k个结点 |
JT15 | 反转链表 |
JT16 | 合并两个排序的链表 |
JT25 | 复杂链表的复制 |
JT26 | 二叉搜索树与双向链表 |
树
剑指offer题号 | 题解地址 |
---|---|
JT4 | 重建二叉树 |
JT17 | 树的子结构 |
JT18 | 二叉树的镜像 |
JT22 | 从上往下打印二叉树 |
JT23 | 二叉搜索树的后序遍历序列 |
JT24 | 二叉树中和为某一值的路径 |
数学
剑指offer题号 | 题解地址 |
---|---|
JT11 | 二进制中1的个数 |
JT12 | 数值的整数次方 |
JT31 | 从1到n整数中1出现的次数 |
JT33 | 丑数 |
其他知识点
剑指offer题号 | 题解地址 | 知识点 |
---|---|---|
JT5 | 用两个栈实现队列 | 栈 |
JT6 | 旋转数组的最小数字 | 二分 |
JT8 | 跳台阶 | 递归 |
JT9 | 变态跳台阶 | 贪心 |
JT10 | 矩形覆盖 | 递归 |
JT20 | 包含min函数的栈 | 栈 |
JT21 | 栈的压入,弹出序列 | 栈 |
JT29 | 最小的K个数 | 快排 |
JT30 | 连续子数组的最大和 | 动态规划 |