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