解决算法模板C++代码没有样式问题

This commit is contained in:
Luo
2021-10-12 14:54:53 +08:00
committed by GitHub
parent 7bb935cfaa
commit f70dc0595a

View File

@ -8,7 +8,7 @@
## 二分查找法
```
```c++
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
@ -33,7 +33,7 @@ public:
## KMP
```
```c++
void kmp(int* next, const string& s){
next[0] = -1;
int j = -1;
@ -53,7 +53,7 @@ void kmp(int* next, const string& s){
二叉树的定义:
```
```c++
struct TreeNode {
int val;
TreeNode *left;
@ -65,7 +65,7 @@ struct TreeNode {
### 深度优先遍历(递归)
前序遍历(中左右)
```
```c++
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
vec.push_back(cur->val); // 中 ,同时也是处理节点逻辑的地方
@ -74,7 +74,7 @@ void traversal(TreeNode* cur, vector<int>& vec) {
}
```
中序遍历(左中右)
```
```c++
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal(cur->left, vec); // 左
@ -83,7 +83,7 @@ void traversal(TreeNode* cur, vector<int>& vec) {
}
```
后序遍历(左右中)
```
```c++
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal(cur->left, vec); // 左
@ -97,7 +97,7 @@ void traversal(TreeNode* cur, vector<int>& vec) {
相关题解:[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md)
前序遍历(中左右)
```
```c++
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
@ -123,7 +123,7 @@ vector<int> preorderTraversal(TreeNode* root) {
```
中序遍历(左中右)
```
```c++
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result; // 存放中序遍历的元素
stack<TreeNode*> st;
@ -148,7 +148,7 @@ vector<int> inorderTraversal(TreeNode* root) {
```
后序遍历(左右中)
```
```c++
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
@ -176,7 +176,7 @@ vector<int> postorderTraversal(TreeNode* root) {
相关题解:[0102.二叉树的层序遍历](https://programmercarl.com/0102.二叉树的层序遍历.html)
```
```c++
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
@ -212,7 +212,7 @@ vector<vector<int>> levelOrder(TreeNode* root) {
### 二叉树深度
```
```c++
int getDepth(TreeNode* node) {
if (node == NULL) return 0;
return 1 + max(getDepth(node->left), getDepth(node->right));
@ -221,7 +221,7 @@ int getDepth(TreeNode* node) {
### 二叉树节点数量
```
```c++
int countNodes(TreeNode* root) {
if (root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
@ -229,7 +229,7 @@ int countNodes(TreeNode* root) {
```
## 回溯算法
```
```c++
void backtracking(参数) {
if (终止条件) {
存放结果;
@ -247,7 +247,7 @@ void backtracking(参数) {
## 并查集
```
```c++
int n = 1005; // 更具题意而定
int father[1005];