2.3.4 树

遍历:前中后序,宽度优先。

二叉树的特例:二叉搜索树、堆(最大堆和最小堆,用于找最值)、红黑树(c++ STL中的很多数据结果就是基于这实现的);

题7-重建二叉树:递归,设置四个位点;

  1. class Solution {
  2. public:
  3. TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
  4. if(pre.size()!=vin.size()||vin.size()==) return nullptr;
  5. return Construct(,pre.size()-,,vin.size()-,pre,vin);
  6. }
  7.  
  8. TreeNode* Construct(int prestart,int preend,int vinstart,int vinend,const vector<int> &pre,const vector<int> &vin){
  9. if(prestart>preend) return nullptr;
  10. TreeNode* root=new TreeNode(pre[prestart]);
  11. for(int i=vinstart;i<=vinend;i++)
  12. if(pre[prestart]==vin[i]){
  13. root->left=Construct(prestart+,prestart+i-vinstart,vinstart,i-,pre,vin);
  14. root->right=Construct(prestart+i-vinstart+,preend,i+,vinend,pre,vin);
  15. break;
  16. }
  17. return root;
  18. }
  19. };

题8-二叉树的下一个节点

  1. class Solution {
  2. public:
  3. TreeLinkNode* GetNext(TreeLinkNode* pNode)
  4. {
  5. if(pNode==nullptr) return nullptr;
  6. if(pNode->right!=nullptr){
  7. pNode=pNode->right;
  8. while(pNode->left!=nullptr){
  9. pNode=pNode->left;
  10. }
  11. return pNode;
  12. }
  13. else{
  14. if(pNode->next==nullptr)
  15. return nullptr;
  16. else{
  17. if(pNode==pNode->next->left)
  18. return pNode->next;
  19. else{
  20. while(pNode->next!=nullptr){
  21. if(pNode==pNode->next->left)
  22. return pNode->next;
  23. pNode=pNode->next;
  24. }
  25. return nullptr;
  26. }
  27. }
  28. }
  29. }
  30. };

2.3.5 栈和队列

题9-两个栈实现队列:一个用于插入,一个用于删除,增加判空操作,每次插入/删除操作后只有一个栈是有数据的;

2.4 算法和数据结构

递归/循环,排序/查找,搜索路径(回溯法),最优解(动态规划),贪心,位运算(与、或、异或、左右移)

2.4.1 递归和循环

如果面试官没有要求,多采用递归;递归存在时间效率和调用栈溢出的问题;

题10-斐波那契数列:递归效率低,采用循环O(n),也可以用到数学公式用递归O(logn),青蛙跳和格子都是这个问题;

2.4.2 查找和排序

顺序查找、二分、哈希表查找、二叉排序树

交换使用swap函数,使用随机数配合递归来进行快排;

面试官的交流:要问排序的是什么数据、数据量,能用多少辅助空间?明确场景

题11-旋转数组的最小数字:O(n)到O(logn),使用二分,但是要有特殊处理,例如如果两个位点元素相等,则内部的应该进行顺序查找;

  1. class Solution {
  2. public:
  3. int minNumberInRotateArray(vector<int> rotateArray) {
  4. if(rotateArray.size()==0) return 0;
  5. int low=0,high=rotateArray.size()-1,mid=(low+high)/2;
  6. if(rotateArray[low]<rotateArray[high]) return rotateArray[low];
  7. if(high<2) return rotateArray[high];
  8. while(low!=high-1){
  9. if(rotateArray[low]<rotateArray[high]) return rotateArray[low];
  10. if(rotateArray[low]==rotateArray[mid]&&rotateArray[mid]==rotateArray[high]){
  11. int min=rotateArray[low];
  12. for(int i=low+1;i<=high;i++)
  13. if(min>rotateArray[i])
  14. min=rotateArray[i];
  15. return min;
  16. }
  17. if(rotateArray[low]>rotateArray[mid]){
  18. //low=low+1;
  19. high=mid;
  20. mid=(low+high)/2;
  21. }
  22. if(rotateArray[high]<rotateArray[mid]){
  23. low=mid;
  24. mid=(low+high)/2;
  25. }
  26. }
  27. return rotateArray[low+1];
  28. }
  29. };

  

2.4.3 回溯法

适合用递归实现,其实也可以用堆栈。

题13-机器人的运动规划:本来觉得两轮遍历就可以找到不可以到达的格子,后来发现是不对的,所以还是用了回溯去找

  1. class Solution {
  2. public:
  3. int movingCount(int threshold, int rows, int cols)
  4. {
  5. if(threshold<=0||rows<=0||cols<=0) return 0;
  6. bool *visited=new bool[rows*cols];
  7. for(int i=0;i<rows*cols;i++)
  8. visited[i]=false;
  9. int res=getCount(rows,cols,0,0,threshold,visited);
  10. delete[] visited;
  11. return res;
  12. }
  13. int getCount(int rows,int cols,int row,int col,int threshold,bool* visited){
  14. int count=0;
  15. if(!visited[row*cols+col]&&(getSum(row,col)<=threshold)&&row>=0&&col>=0&&row<rows&&col<cols){
  16. visited[row*cols+col]=true;
  17. count=1+getCount(rows,cols,row-1,col,threshold,visited)+getCount(rows,cols,row+1,col,threshold,visited)+getCount(rows,cols,row,col-1,threshold,visited)+getCount(rows,cols,row,col+1,threshold,visited);
  18. }
  19. return count;
  20. }
  21. int getSum(int rows,int cols){
  22. int sum=0;
  23. while(rows>0||cols>0){
  24. sum+=(rows%10+cols%10);
  25. rows/=10;
  26. cols/=10;
  27. }
  28. return sum;
  29. }
  30. };

2.4.4 动态规划与贪婪算法

动规:

  • 分解成很多子问题;
  • 整体的问题依赖于子问题
  • 子问题之间还有相互重叠的更小的子问题

从上往下分析,从下往上求解(顺序先计算出小问题的最优解并存储下来)

题-剪绳子:使用动态规划来做(两个循环 O(n方));用贪婪法(o(1)的时间和空间,需要证明);

2.4.5 位运算

与、或、异或、左右移(计算效率比乘除高);

注意右移时如果是负数的话补的是1,还有把整数减去1之后与原来的数做位与运算后结果相当于把整数的二进制表示中最右边的1变为0,很多二进制问题可以用这个套路;

题-二进制中1的个数

  1. class Solution {
  2. public:
  3. int NumberOf1(int n) {
  4. int count=0;
  5. while(n){
  6. count++;
  7. n=n&(n-1);
  8. }
  9. return count;
  10. }
  11. };

  

剑指offer中数据结构与算法部分学习的更多相关文章

  1. 剑指offer中经典的算法题之从头到尾打印链表

    话不多说上代码: 我自己的算法是: /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int ...

  2. 剑指Offer——知识点储备-常用算法

    剑指Offer--知识点储备-常用算法 快速排序 注:若排序是有序的,采用快排,则退化为冒泡排序. 解决这个问题,采用两个选取基准的方法 (1)随机选取基数(在这个区间内随机取一个数) 出现的恶劣情况 ...

  3. 剑指offer之O(1)算法删除指针所指向的节点

    题目如图: 1.把要删除pToBeDeleted的节点的后面节点覆盖点要删除的节点pToBeDeleted 2.要考虑如果删除的节点是最后一个节点怎么办 3.要考虑如果总共只有一个节点,删除的是头结点 ...

  4. 剑指offer——已知二叉树的先序和中序排列,重构二叉树

    这是剑指offer中关于二叉树重构的一道题.题目原型为: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2, ...

  5. 【剑指Offer学习】【面试题:二维数组中的查找】PHP实现

    最近一直看剑指Offer.里面很多算法题.于是就想着用PHP来显示一下. 题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的 ...

  6. 《剑指offer》算法题第一天

    按照个人计划,从今天开始做<剑指offer>上面的算法题,练习平台为牛客网,上面对每道题都有充分的测试实例,感觉还是很不错的.今天下午做了四道题,分别为: 1. 二叉树的深度(书55题) ...

  7. 面试题目——《剑指Offer》

    1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...

  8. 剑指Offer——归并排序思想应用

    剑指Offer--归并排序思想应用 前言 在学习排序算法时,初识归并排序,从其代码量上感觉这个排序怎么这么难啊.其实归并排序的思想很简单:将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列 ...

  9. 7、斐波那契数列、跳台阶、变态跳台阶、矩形覆盖------------>剑指offer系列

    题目:斐波那契数列 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). f(n) = f(n-1) + f(n-2) 基本思路 这道题在剑指offe ...

随机推荐

  1. [BZOJ2821]作诗

    description 在线询问区间内出现次数为正偶数的数的种数. data range \[n,m\le 10^5\] solution 分块大法好 首先离散化权值 这种对于权值做询问并且询问放在一 ...

  2. Android 打开照相机、获取相册图片、获取图片并裁减

    一.调用照相机 注:surfaceView在当Activity不在前台的时候,会被销毁(onPause方法之后,执行销毁方法)当Activity回到前台时,在Activity执行onResume方法之 ...

  3. CF97B:Superset——题解

    http://codeforces.com/problemset/problem/97/B 题目大意:给n个点,添加一些点,使得任意两个点: 1.在同一条线上 2.以它们为顶点构成的矩形上有其他点. ...

  4. push与createElement性能比较

    下面的实验是验证push()函数与CreateElement()函数在创建HTML元素的效率.可以看出,实用push()确实效率要比后者要高,不过究竟可以高出多少,还需要有专研精神的朋友去测试了,这里 ...

  5. 我们自己写的solr查询的代码作为search项目中的dao

    我们自己写的solr查询的代码作为search项目中的dao,但是启动时会报错: 其实就是说 searchServiceImpl 中我们 Autowired 的 SearchDao 类 spring ...

  6. [10.18模拟赛] 序列 (DP)

    [10.18模拟赛] 序列 题目描述 山山有一个整数序列s1,s2,-,sn,其中1≤si≤k. 求出有多少个准确移除m个元素后不同的序列.答案模(1e9+7) 输入 输入包括几个测试用例,并且由文件 ...

  7. last-child 选择器

    <!DOCTYPE html> <html> <head> <style> p:last-child //p的父类 的子类下最后一个,就是p兄弟层的最后 ...

  8. mysql按月统计六个月内不同类型订单的成交金额

    mysql按月统计六个月内不同类型订单的成交金额 创建数据库 CREATE DATABASE test; 创建订单表 CREATE TABLE `t_order` ( `id` ) NOT NULL ...

  9. 使用html5的Geolocation API实现定位

    公司有个需求,需要获取用户的位置,所以看了下html5的Geolocation 这个新东西,发现挺好用的. <!DOCTYPE html> <html> <body> ...

  10. [LeetCode] 14. Longest Common Prefix ☆

    Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...