算法与数据结构
Panix2n_n
这个作者很懒,什么都没留下…
展开
-
算法入门之一—链表与数组
一、链表与数组链表的优势在插入元素方面,并且节省空降,但查询速度慢,仅支持顺序访问数组的优势在查询元素方面,支持随机访问和顺序访问 数组链表读取O(1)O(n)插入O(n)O(1)删除O(n)O(1)二、递归1、栈:后进先出2、递归:一种直接或者间接的调用原算法本身的一种算法3、编写涉及数组的递归函数时,基线条件通常是数组为空或只包含一个元素4、每个递归函数都有两个条件:基线条件和递归条件5、所有...原创 2018-06-06 10:48:42 · 168 阅读 · 0 评论 -
二叉树前序、中序、后序遍历及推导方法
目录二叉树的遍历方法推导遍历结果1、已知前序遍历和中序遍历2、已知中序遍历和后序遍历3、已知前序遍历和后序遍历,无法确定一颗唯一的二叉树参考文献 二叉树的遍历方法前序遍历:根左右。先打印,再遍历左子树,再遍历右子树; 中序遍历:左根右。先遍历左子树,再打印,再遍历右子树; 后序遍历:左右根。先遍历左子树,再遍历右子树,再打印。采用递归思想实现:/**...原创 2018-09-10 20:39:17 · 22304 阅读 · 5 评论 -
快速排序
将最左边的点设置为基准点,小的放到左边,大的放到右边,然后递归直到left>=right为止。#include <iostream>using namespace std;void quickSort(int array[], int left, int right){ if (left >= right) { return;...原创 2018-10-13 08:51:03 · 160 阅读 · 0 评论 -
二叉树的非递归遍历
节点:class Node{public: int data; Node* left; Node* right;};非递归前序遍历:void preOrder1(Node* root){ if (root == nullptr) { return; } stack<Node*> nStac...原创 2018-10-09 11:52:50 · 123 阅读 · 0 评论 -
c++ 实现全排列
中心思想:递归求子数组的全排列。具体操作:将每个元素都与子数组的第一个元素交换, 再求剩余部分的全排列, 求完后再交换会来,复原原数组。中止条件:low==high时,输出一种排列情况。#include <iostream>using namespace std;void swap(int &a,int &b){ int temp=a...原创 2018-10-10 11:11:20 · 5017 阅读 · 0 评论 -
插入排序、希尔排序、快速排序
插入排序:void insertSort(int array[], int n){ int temp = 0; int j = 0; for (int i = 1; i < n; i++) { if (array[i] < array[i - 1])//当前值小于有序序列的最大值 { tem...原创 2018-10-17 17:54:08 · 211 阅读 · 0 评论 -
C++ 素数
#include <iostream>#include <string.h>#include <math.h>using namespace std;int primeNum(int n){ if (n <= 1) { return 0; } int mid = sqrt(n); for...原创 2018-10-27 09:54:05 · 430 阅读 · 0 评论 -
动态规划 - 最长公共子串
设dp[i][j]为当公共子串的最后一位字母为str1[i]和str2[j]时,公共子串的长度。状态转移方程:当str1[i] == str2[j]时, dp[i][j] = dp[i - 1][j - 1] + 1; 当str1[i] != str2[j]时 , dp[i][j] = 0; #include<iostream>#include<string.h&...原创 2018-10-27 10:17:48 · 345 阅读 · 0 评论