算法

查找:二分查找

排序

  • 快排
    • 基于快排思想解决的问题
    • partition,
      • 第k大的数字
  • 归并
  • 几种排序算法的时间复杂度,稳定性等

DP

斐波那契数列

  • 递归导致大量重复计算
  • 循环,保存中间结果
变种
  • 跳台阶
  • 小矩形覆盖大矩形

连续子数组的最大和

求最大路径和

求最长公共子序列

排列,组合,回溯法

ex.1

/*
*  打印出一个字符串的全部排列。
*/

ex.2

/*
*  打印出一个字符串的组合。
*/

排列

  • 全排列:从第一个数字起,每个数字分别与它后面的数字交换
  • 去重全排列:从第一个数字起,每个数组分别与它后面非重复出现的数字交换

组合

  • 基于递归
  • 基于位图

ex.3

/*
*  输入一个含有8个数字的数组,判断有没有可能把这8个数字分配放到正方体
*  的8个顶点上,是得正方体上三组相对的面上的4个顶点的和都相等
*/
//求8个数字的全部排列,然后判断排列是否满足题目给出的条件

ex.4

/*
*  8皇后问题。在8*8的棋盘上拜放8个皇后,使其不能相互攻击,即8个皇后
*  不能处于同一行,同一列或者同一对角线上。
*/
//初始化一个columnIndex[8]数组,第i个数表示位于第i行的列数,因此用0-7来
//初始化数组,然后做全排列,逐个判断是否满足不同行、列、对角线的要求



//初始化一个columnIndex[8]数组,第i个数表示位于第i行的列数
// 使用回溯法,从第一行第一列初始化数组

回溯法

//8皇后
int C[8] = {0};
cout << search(0)  << endl;
 int tot = 0;
void search(int cur) {
    if(cur == 8) tot++;
    else for (int i = 0; i < 8;++i) {
        int ok = 1;
        C[cur] = i;
        for(int j = 0; j< cur; ++j) {
            if(C[cur]==C[j] || cur-C[cur]==j-C[j] || cur+C[cur]==j+C[j]){
              of = 0; break;  
          }
        }
        if(ok)  search(cur+1);
    }
}
//矩阵中的路径
//从任意格子出发,检查是否满足路径,同数组表示访问的路径表示已经到达过的位置以及当前到达的位置
//先检查当前格子,是否满足条件,满足则向另外4个方向出发,不满足则返回
   bool hasPath(char* matrix, int rows, int cols, char* str)
    {
        if(matrix == NULL || rows < 1 || cols < 1 || str == NULL)
            return false;
        bool *visited= new bool[rows * cols];
        memset(visited, 0, rows*cols*sizeof(bool));
        
        int pathLength = 0;
        for(int row = 0;row < rows;++row) {
            for(int col = 0;col < cols;++col) {
                if(hasPathCore(matrix,rows,cols,row,col,str,pathLength,visited)) {
                    delete [] visited;
                    return true;
                }
            }
        }
        delete [] visited;
        return false;
    }
    bool hasPathCore(char* matrix, int rows, int cols,int row,int col, 
                     char* str,int& pathLength,bool* visited){
        if(str[pathLength] == '\0')
            return true;
        bool hasPath = false;
        if(row >= 0 && row < rows && col >=0 && col < cols
          && matrix[row * cols + col] == str[pathLength]  && !visited[row * cols + col]) {
            ++pathLength;
            visited[row * cols + col] = true;
            hasPath = hasPathCore(matrix,rows,cols,row+1,col,str,pathLength,visited)
                   || hasPathCore(matrix,rows,cols,row,col+1,str,pathLength,visited)
                   || hasPathCore(matrix,rows,cols,row-1,col,str,pathLength,visited)
                   || hasPathCore(matrix,rows,cols,row,col-1,str,pathLength,visited);
            if(!hasPath) {
                --pathLength;
                visited[row * cols + col] = false;
            }
        }
        return hasPath;
    }

// 机器人的运行范围
// 使用一个数组来记录已访问的位置,
//  从(0,0)出发, 每次移动位置都要检查是否满足要求。
// 如果满足要求则设置记录数组,并且增加路程

    int movingCount(int threshold, int rows, int cols)
    {
        if(threshold <= 0 || rows <= 0 || cols <= 0)
            return 0;
        bool *visited = new bool[rows * cols];
        for(int i = 0; i < rows * cols; ++i)
            visited[i] = false;
        int count = movingCountCore(threshold,rows,cols,0,0,visited);
        delete [] visited;
        return count;
    }
    int movingCountCore(int threshold, int rows, int cols,int row,int col,bool *visited){
        int pathLength = 0;
        if(row >=0 && row < rows && col >= 0 && col < cols 
           && !visited[row * cols + col] && checkOk(threshold, row, col)) {
            visited[row * cols + col] = true;
            pathLength = 1 + movingCountCore(threshold,rows,cols,row,col+1,visited)
                       + movingCountCore(threshold,rows,cols,row+1,col,visited)
                       + movingCountCore(threshold,rows,cols,row-1,col,visited)
                       + movingCountCore(threshold,rows,cols,row,col-1,visited);
         
        }
        return pathLength;
    }
    bool checkOk(int threshold, int row, int col)
    {
        int tmp = 0;
        while(row > 0){
            tmp += row % 10;
            row = row / 10;
        }
        while(col > 0){
            tmp += col % 10;
            col = col / 10;
        }
        if(tmp <= threshold)
            return true;
        return false;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 5,152评论 0 12
  • 概述:排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    每天刷两次牙阅读 3,742评论 0 15
  • 概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    蚁前阅读 5,219评论 0 52
  • 本文出自 Eddy Wiki ,转载请注明出处:http://eddy.wiki/interview-code.h...
    eddy_wiki阅读 9,366评论 0 30
  • 1、Kmp匹配算法:开始的时候还是遍历targetstring ,根据findstring的每个字符去查找,这样需...
    夺光阅读 358评论 0 0