![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
关东老樊
爱思考的码农
展开
-
递归的本质理解
什么是递归函数里面调用函数本身,这就是递归public int factorial(int n) { if (n < =1) { return 1; } return n * factorial(n - 1)}先有【递】,再有【归】【递】是将问题拆分成子问题来解决,子问题再分成【子问题的子问题】…,一直到被拆分的子问题无需再拆分成更小的子问题(可以求解)【归】是指最小的子问题解决了,那么它的上一层子问题也解决了,上一层的上一层子问题也解决了,…,原创 2022-05-06 20:13:31 · 151 阅读 · 0 评论 -
归并排序,C++实现
归并排序采用分治法的一个典型应用;实现方法有两种:1、自上而下的递归,所有递归的方法都可以用迭代重写2、自下而上的迭代c++代码,递归版#include <iostream>#include <vector>#include <algorithm>using namespace std;//Merge(vec, start, mid, end);void Merge(vector<int>& vec, int start,原创 2022-04-05 10:16:45 · 1150 阅读 · 0 评论 -
二叉树的前序、中序、后序遍历,递归和迭代两种解法(C++)
二叉树的遍历递归解法前序遍历:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr),原创 2022-01-11 12:00:08 · 552 阅读 · 0 评论 -
快速排序C++实现
方式一 交换元素void QuickSort(int a[],int first, int end){ if (first > end) return; int temp = a[first];//temp存的就是基准数 int i = first; int j = end; while (i < j) { //顺序很重要,要先从右边开始找 while (j > i&&...原创 2021-11-15 13:07:47 · 832 阅读 · 0 评论 -
二分查找C++实现
非递归方式代码#include <iostream>using namespace std;int search(int* a, int len, int target){ int left = 0; int right = len - 1; while (left <= right) { int mid = left + (right - left) / 2; if (a[mid] == target) { return a[mid];原创 2021-12-05 11:20:53 · 186 阅读 · 0 评论