mirror of
				https://git.wolves.top/wolves/leetcode.git
				synced 2025-11-04 09:16:32 +08:00 
			
		
		
		
	init
This commit is contained in:
		
							
								
								
									
										29
									
								
								else/DivConvert.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								else/DivConvert.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/2.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_DIVCONVERT_H
 | 
			
		||||
#define LEECODE_C_DIVCONVERT_H
 | 
			
		||||
 | 
			
		||||
#include <stack>
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
string decimalToBinaryUsingEuclidean(int target) {
 | 
			
		||||
    stack<int> s;
 | 
			
		||||
    string result = "";
 | 
			
		||||
    while (target) {
 | 
			
		||||
        s.push(target % 2);
 | 
			
		||||
        target /= 2;
 | 
			
		||||
    }
 | 
			
		||||
    while (!s.empty()) {
 | 
			
		||||
        result.append(to_string(s.top()));
 | 
			
		||||
        s.pop();
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// test : cout << decimalToBinaryUsingEuclidean(555) << endl;
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_DIVCONVERT_H
 | 
			
		||||
							
								
								
									
										0
									
								
								else/Heap/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								else/Heap/README.md
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										34
									
								
								else/Heap/test.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								else/Heap/test.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/18.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_TEST_H_P
 | 
			
		||||
#define LEECODE_C_TEST_H_P
 | 
			
		||||
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void testPQ() {
 | 
			
		||||
    priority_queue<int, deque<int>, less<>> maxHeap;
 | 
			
		||||
    maxHeap.push(1);
 | 
			
		||||
    maxHeap.push(10);
 | 
			
		||||
    maxHeap.push(3);
 | 
			
		||||
    maxHeap.push(3);
 | 
			
		||||
    maxHeap.push(3);
 | 
			
		||||
    cout << maxHeap.top() << endl;
 | 
			
		||||
    maxHeap.pop();
 | 
			
		||||
    maxHeap.push(4);
 | 
			
		||||
    cout << maxHeap.top() << endl;
 | 
			
		||||
    maxHeap.pop();
 | 
			
		||||
    maxHeap.push(2);
 | 
			
		||||
    cout << maxHeap.top() << endl;
 | 
			
		||||
    priority_queue<int, vector<int>, greater<>> minHeap;
 | 
			
		||||
    minHeap.push(20);
 | 
			
		||||
    minHeap.push(10);
 | 
			
		||||
    minHeap.push(30);
 | 
			
		||||
    cout << minHeap.top() << endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_TEST_H_P
 | 
			
		||||
							
								
								
									
										23
									
								
								else/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								else/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
# ELSE
 | 
			
		||||
 | 
			
		||||
-
 | 
			
		||||
    - [X] [用栈实现辗转相除法](./DivConvert.h) - P59
 | 
			
		||||
-
 | 
			
		||||
    - [X] [对称序列](./symmetry.h)
 | 
			
		||||
-
 | 
			
		||||
    - [X] [括号匹配](./kuohao.h) - P63
 | 
			
		||||
-
 | 
			
		||||
    - [ ] 二叉树计算
 | 
			
		||||
-
 | 
			
		||||
    - [X] [二叉树代码 递归 + 非递归前中后序遍历](./traverse.h)
 | 
			
		||||
-
 | 
			
		||||
    - [X] [快速排序](./sorts.h)
 | 
			
		||||
 | 
			
		||||
    - [ ] [堆排序](./sorts.h)
 | 
			
		||||
-
 | 
			
		||||
    - [X] [图](./structs/Graph.h)
 | 
			
		||||
 | 
			
		||||
    - [X] [深度优先遍历](./structs/Graph.h)
 | 
			
		||||
 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										31
									
								
								else/demo/1.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								else/demo/1.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2024/1/8.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_1_H
 | 
			
		||||
#define LEECODE_C_1_H
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void test1() {
 | 
			
		||||
    //方式一
 | 
			
		||||
    int a;
 | 
			
		||||
    cout << "输入长度:";
 | 
			
		||||
    cin >> a;
 | 
			
		||||
    int *p = (int *) malloc(a * sizeof(int)); //c写法在运行中分配空间创建指定长度数组
 | 
			
		||||
    p[a - 1] = 1;
 | 
			
		||||
    cout << p[a-1] << endl;
 | 
			
		||||
 | 
			
		||||
    //方式2 推荐
 | 
			
		||||
    vector<int> b; //c++ 向量,可以看作是动态数组,自动扩充容量
 | 
			
		||||
    vector<vector<int>> c; //二维动态数组
 | 
			
		||||
    b.push_back(1);
 | 
			
		||||
    b.push_back(2);
 | 
			
		||||
    b.size(); //获取当前长度
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_1_H
 | 
			
		||||
							
								
								
									
										24
									
								
								else/demo/2.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								else/demo/2.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2024/1/11.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_2_H
 | 
			
		||||
#define LEECODE_C_2_H
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void test1(int *p, int index) {
 | 
			
		||||
    cout << p[index];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void run() {
 | 
			
		||||
    int arr[3];
 | 
			
		||||
    arr[0] = 1;
 | 
			
		||||
    arr[1] = 2;
 | 
			
		||||
    arr[2] = 3;
 | 
			
		||||
    test1(arr, 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_2_H
 | 
			
		||||
							
								
								
									
										52
									
								
								else/kuohao.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								else/kuohao.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/4.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_KUOHAO_H
 | 
			
		||||
#define LEECODE_C_KUOHAO_H
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <stack>
 | 
			
		||||
 | 
			
		||||
bool isKuoHao(std::string target) {
 | 
			
		||||
    std::stack<char> tk;
 | 
			
		||||
    for (int i = 0; i < target.length(); ++i) {
 | 
			
		||||
        if (tk.empty()) {
 | 
			
		||||
            tk.push(target[i]);
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        char temp;
 | 
			
		||||
        switch (target[i]) {
 | 
			
		||||
            case '{':
 | 
			
		||||
                temp = '}';
 | 
			
		||||
                break;
 | 
			
		||||
            case '}':
 | 
			
		||||
                temp = '{';
 | 
			
		||||
                break;
 | 
			
		||||
            case '[':
 | 
			
		||||
                temp = ']';
 | 
			
		||||
                break;
 | 
			
		||||
            case ']':
 | 
			
		||||
                temp = '[';
 | 
			
		||||
                break;
 | 
			
		||||
            case '(':
 | 
			
		||||
                temp = ')';
 | 
			
		||||
                break;
 | 
			
		||||
            case ')':
 | 
			
		||||
                temp = '(';
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                temp = 'x';
 | 
			
		||||
        }
 | 
			
		||||
        if (temp == tk.top()) {
 | 
			
		||||
            tk.pop();
 | 
			
		||||
        } else {
 | 
			
		||||
            tk.push(target[i]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return tk.empty();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// cout << isKuoHao("{}(({}))") << endl;
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_KUOHAO_H
 | 
			
		||||
							
								
								
									
										31
									
								
								else/main.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								else/main.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/12.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_MAIN_H
 | 
			
		||||
#define LEECODE_C_MAIN_H
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include "./structs/Tree.h"
 | 
			
		||||
#include "./traverse.h"
 | 
			
		||||
#include "./Heap/test.h"
 | 
			
		||||
#include "./sorts.h"
 | 
			
		||||
#include "./structs/Graph.h"
 | 
			
		||||
#include "./topic/2019.h"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void run()
 | 
			
		||||
{
 | 
			
		||||
    vector<int> arr({3, 2, 2, 4, 6, 2, 3});
 | 
			
		||||
    odd_even(arr);
 | 
			
		||||
    for (int i : arr)
 | 
			
		||||
    {
 | 
			
		||||
        cout << i << " ";
 | 
			
		||||
    }
 | 
			
		||||
    cout << endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // LEECODE_C_MAIN_H
 | 
			
		||||
							
								
								
									
										171
									
								
								else/sorts.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										171
									
								
								else/sorts.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,171 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/19.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_SHELLSORT_H
 | 
			
		||||
#define LEECODE_C_SHELLSORT_H
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <random>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void shellSort(vector<int> &arr) {
 | 
			
		||||
    int n = arr.size();
 | 
			
		||||
 | 
			
		||||
    // 选择增量序列,可以根据需要选择不同的序列
 | 
			
		||||
    for (int gap = n / 2; gap > 0; gap /= 2) {
 | 
			
		||||
        // 使用插入排序对每个子数组进行排序
 | 
			
		||||
        for (int i = gap; i < n; ++i) {
 | 
			
		||||
            int temp = arr[i];
 | 
			
		||||
            int j = i;
 | 
			
		||||
            while (j >= gap && arr[j - gap] > temp) {
 | 
			
		||||
                arr[j] = arr[j - gap];
 | 
			
		||||
                j -= gap;
 | 
			
		||||
            }
 | 
			
		||||
            arr[j] = temp;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 快速排序 - start
 | 
			
		||||
int partition(vector<int> &arr,int left,int right){
 | 
			
		||||
    int sign = arr[left];
 | 
			
		||||
    while(left < right){
 | 
			
		||||
        while(left < right && sign <= arr[right]){
 | 
			
		||||
            right--;
 | 
			
		||||
        }
 | 
			
		||||
        swap(arr[left],arr[right]);
 | 
			
		||||
        while(left < right && arr[left] <= sign){
 | 
			
		||||
            left++;
 | 
			
		||||
        }
 | 
			
		||||
        swap(arr[left],arr[right]);
 | 
			
		||||
    }
 | 
			
		||||
    return left;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void splits(vector<int> &arr,int left,int right){
 | 
			
		||||
    if (left < right)
 | 
			
		||||
    {
 | 
			
		||||
        int norm = partition(arr,left,right);
 | 
			
		||||
        splits(arr,left,norm - 1);
 | 
			
		||||
        splits(arr,norm + 1,right);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void quickSort(vector<int> &arr) {
 | 
			
		||||
    splits(arr, 0, arr.size() - 1);
 | 
			
		||||
}
 | 
			
		||||
// 快速排序 - end
 | 
			
		||||
 | 
			
		||||
// 堆排序 - start small -> large
 | 
			
		||||
void maxHeapify(vector<int> &arr, int n, int i) {
 | 
			
		||||
    int largest = i;
 | 
			
		||||
    int left = i * 2 + 1;
 | 
			
		||||
    int right = i * 2 + 2;
 | 
			
		||||
    if (left < n && arr[left] > arr[largest]) {
 | 
			
		||||
        largest = left;
 | 
			
		||||
    }
 | 
			
		||||
    if (right < n && arr[right] > arr[largest]) {
 | 
			
		||||
        largest = right;
 | 
			
		||||
    }
 | 
			
		||||
    if (largest != i) {
 | 
			
		||||
        swap(arr[i], arr[largest]);
 | 
			
		||||
        maxHeapify(arr, n, largest);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void heapSort(vector<int> &arr) {
 | 
			
		||||
    auto n = arr.size();
 | 
			
		||||
    for (int i = n / 2 - 1; i >= 0; --i) {
 | 
			
		||||
        maxHeapify(arr, n, i);
 | 
			
		||||
    }
 | 
			
		||||
    for (int i = n - 1; i > 0; i--) {
 | 
			
		||||
        swap(arr[0], arr[i]);
 | 
			
		||||
        maxHeapify(arr, i, 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
// 堆排序 - end small -> large
 | 
			
		||||
 | 
			
		||||
// 归并排序
 | 
			
		||||
void merge(std::vector<int>& arr, int left, int middle, int right) {
 | 
			
		||||
    int n1 = middle - left + 1;
 | 
			
		||||
    int n2 = right - middle;
 | 
			
		||||
 | 
			
		||||
    std::vector<int> leftArr(n1);
 | 
			
		||||
    std::vector<int> rightArr(n2);
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < n1; i++) {
 | 
			
		||||
        leftArr[i] = arr[left + i];
 | 
			
		||||
    }
 | 
			
		||||
    for (int i = 0; i < n2; i++) {
 | 
			
		||||
        rightArr[i] = arr[middle + 1 + i];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int i = 0, j = 0, k = left;
 | 
			
		||||
 | 
			
		||||
    while (i < n1 && j < n2) {
 | 
			
		||||
        if (leftArr[i] <= rightArr[j]) {
 | 
			
		||||
            arr[k] = leftArr[i];
 | 
			
		||||
            i++;
 | 
			
		||||
        } else {
 | 
			
		||||
            arr[k] = rightArr[j];
 | 
			
		||||
            j++;
 | 
			
		||||
        }
 | 
			
		||||
        k++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (i < n1) {
 | 
			
		||||
        arr[k] = leftArr[i];
 | 
			
		||||
        i++;
 | 
			
		||||
        k++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (j < n2) {
 | 
			
		||||
        arr[k] = rightArr[j];
 | 
			
		||||
        j++;
 | 
			
		||||
        k++;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void mergeSort(std::vector<int>& arr, int left, int right) {
 | 
			
		||||
    if (left < right) {
 | 
			
		||||
        int middle = left + (right - left) / 2;
 | 
			
		||||
        mergeSort(arr, left, middle);
 | 
			
		||||
        mergeSort(arr, middle + 1, right);
 | 
			
		||||
        merge(arr, left, middle, right);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void runS() {
 | 
			
		||||
//    std::random_device rd;
 | 
			
		||||
//    std::mt19937 gen(rd());
 | 
			
		||||
//    std::uniform_int_distribution<int> dis(1, 100);
 | 
			
		||||
    vector<int> nums;
 | 
			
		||||
    nums.reserve(10);
 | 
			
		||||
//    for (int i = 0; i < 10; ++i) {
 | 
			
		||||
//        nums.push_back(dis(gen));
 | 
			
		||||
//    }
 | 
			
		||||
    nums.push_back(11);
 | 
			
		||||
    nums.push_back(6);
 | 
			
		||||
    nums.push_back(9);
 | 
			
		||||
    nums.push_back(23);
 | 
			
		||||
    nums.push_back(6);
 | 
			
		||||
    nums.push_back(11);
 | 
			
		||||
    nums.push_back(26);
 | 
			
		||||
    nums.push_back(40);
 | 
			
		||||
    nums.push_back(8);
 | 
			
		||||
    nums.push_back(95);
 | 
			
		||||
    for (int i: nums) {
 | 
			
		||||
        cout << i << " ";
 | 
			
		||||
    }
 | 
			
		||||
    cout << endl;
 | 
			
		||||
    shellSort(nums);
 | 
			
		||||
    for (int i: nums) {
 | 
			
		||||
        cout << i << " ";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_SHELLSORT_H
 | 
			
		||||
							
								
								
									
										181
									
								
								else/structs/Graph.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								else/structs/Graph.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,181 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/21.
 | 
			
		||||
// 以邻接矩阵实现的INT图及其具体操作
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_GRAPH_H
 | 
			
		||||
#define LEECODE_C_GRAPH_H
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <set>
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <random>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <stack>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
class Graph {
 | 
			
		||||
public:
 | 
			
		||||
    int size;
 | 
			
		||||
 | 
			
		||||
    Graph(int n, bool direction) {
 | 
			
		||||
        this->size = n;
 | 
			
		||||
        G = vector<vector<int>>(size, vector<int>(size, 0));
 | 
			
		||||
        visited = vector<int>(size, 0);
 | 
			
		||||
 | 
			
		||||
        random_device rd;
 | 
			
		||||
        mt19937 gen(rd());
 | 
			
		||||
        uniform_int_distribution<int> dis(0, 1);
 | 
			
		||||
 | 
			
		||||
        if (!direction) {
 | 
			
		||||
            for (int i = 0; i < size; ++i) {
 | 
			
		||||
                for (int j = i + 1; j < size; ++j) {
 | 
			
		||||
                    if (dis(gen)) {
 | 
			
		||||
                        G[i][j] = 1;
 | 
			
		||||
                        G[j][i] = 1;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            for (int i = 0; i < size; ++i) {
 | 
			
		||||
                for (int j = 0; j < size; ++j) {
 | 
			
		||||
                    if (i == j) {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    if (dis(gen)) {
 | 
			
		||||
                        G[i][j] = 1;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    0 1 0 0 0 0
 | 
			
		||||
//    1 0 1 1 0 0
 | 
			
		||||
//    0 1 0 0 0 1
 | 
			
		||||
//    0 1 0 0 0 0
 | 
			
		||||
//    0 0 0 0 0 1
 | 
			
		||||
//    0 0 1 0 1 0
 | 
			
		||||
    Graph(int x) {
 | 
			
		||||
        size = 6;
 | 
			
		||||
        G = vector<vector<int>>(size, vector<int>(size, 0));
 | 
			
		||||
        visited = vector<int>(size, 0);
 | 
			
		||||
        vector<vector<int>> Edges;
 | 
			
		||||
        if (x == 1) {
 | 
			
		||||
            Edges.push_back({0, 1});
 | 
			
		||||
            Edges.push_back({1, 2});
 | 
			
		||||
            Edges.push_back({1, 3});
 | 
			
		||||
            Edges.push_back({2, 5});
 | 
			
		||||
            Edges.push_back({4, 5});
 | 
			
		||||
        }
 | 
			
		||||
        if (x == 2) {
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        for (auto Edge: Edges) {
 | 
			
		||||
            G[Edge[0]][Edge[1]] = 1;
 | 
			
		||||
            G[Edge[1]][Edge[0]] = 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Graph *GraphByEdge(vector<vector<int>> &Edges) {
 | 
			
		||||
        for (auto Edge: Edges) {
 | 
			
		||||
            vertex.insert(Edge[1]);
 | 
			
		||||
            vertex.insert(Edge[2]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        size = vertex.size();
 | 
			
		||||
        G = vector<vector<int>>(size, vector<int>(size, 0));
 | 
			
		||||
 | 
			
		||||
        for (auto Edge: Edges) {
 | 
			
		||||
            G[Edge[1]][Edge[2]] = 1;
 | 
			
		||||
        }
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    void printG() {
 | 
			
		||||
        for (int i = 0; i < size; ++i) {
 | 
			
		||||
            for (int j = 0; j < size; ++j) {
 | 
			
		||||
                cout << G[i][j] << " ";
 | 
			
		||||
            }
 | 
			
		||||
            cout << endl;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    queue<int> DFS(int start) {
 | 
			
		||||
        queue<int> Q;
 | 
			
		||||
        stack<pair<int, int>> S;
 | 
			
		||||
        Q.push(start);
 | 
			
		||||
        S.emplace(start, -1);
 | 
			
		||||
        visited[0] = 1;
 | 
			
		||||
        while (!S.empty()) {
 | 
			
		||||
            auto temp = S.top();
 | 
			
		||||
            auto next = NextPoint(temp.first, temp.second);
 | 
			
		||||
            S.top().second = next;
 | 
			
		||||
            if (next == -1) {
 | 
			
		||||
                S.pop();
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            if (visited[next]) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            Q.push(next);
 | 
			
		||||
            visited[next] = 1;
 | 
			
		||||
            S.emplace(next, -1);
 | 
			
		||||
        }
 | 
			
		||||
        if (Q.size() != size) {
 | 
			
		||||
            // 将其他的点再进行DSF
 | 
			
		||||
        }
 | 
			
		||||
        return Q;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    queue<int> BFS(int start) {
 | 
			
		||||
        queue<int> Q;
 | 
			
		||||
        queue<pair<int, int>> S;
 | 
			
		||||
        Q.push(start);
 | 
			
		||||
        S.emplace(start, -1);
 | 
			
		||||
        visited[0] = 1;
 | 
			
		||||
        while (!S.empty()) {
 | 
			
		||||
            auto temp = S.front();
 | 
			
		||||
            auto next = NextPoint(temp.first, temp.second);
 | 
			
		||||
            S.front().second = next;
 | 
			
		||||
            if (next == -1) {
 | 
			
		||||
                S.pop();
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            if (visited[next]) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            Q.push(next);
 | 
			
		||||
            visited[next] = 1;
 | 
			
		||||
            S.emplace(next, -1);
 | 
			
		||||
        }
 | 
			
		||||
        if (Q.size() != size) {
 | 
			
		||||
            // 将其他的点再进行BSF
 | 
			
		||||
        }
 | 
			
		||||
        return Q;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int NextPoint(int vex, int now) {
 | 
			
		||||
        auto row = G[vex];
 | 
			
		||||
        for (int i = now + 1; i < size; ++i) {
 | 
			
		||||
            if (row[i] != 0) {
 | 
			
		||||
                return i;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    vector<vector<int>> G; // adjacent matrix
 | 
			
		||||
    vector<int> visited;
 | 
			
		||||
 | 
			
		||||
    set<int> vertex;   // The collection of vertex
 | 
			
		||||
    set<set<int>> ccp; // The graph has multiple connected components.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_GRAPH_H
 | 
			
		||||
							
								
								
									
										8
									
								
								else/structs/List.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								else/structs/List.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
#ifndef List_h
 | 
			
		||||
#define List_h
 | 
			
		||||
struct ListNode{
 | 
			
		||||
    int value;
 | 
			
		||||
    struct ListNode * next;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //List_h
 | 
			
		||||
							
								
								
									
										94
									
								
								else/structs/Tree.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								else/structs/Tree.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/8/14.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_TREE_H
 | 
			
		||||
#define LEECODE_C_TREE_H
 | 
			
		||||
 | 
			
		||||
struct TreeNode
 | 
			
		||||
{
 | 
			
		||||
    int val;
 | 
			
		||||
    TreeNode *left;
 | 
			
		||||
    TreeNode *right;
 | 
			
		||||
 | 
			
		||||
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
 | 
			
		||||
 | 
			
		||||
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 | 
			
		||||
 | 
			
		||||
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <random>
 | 
			
		||||
#include <queue>
 | 
			
		||||
 | 
			
		||||
TreeNode *createRandomTree(int size)
 | 
			
		||||
{
 | 
			
		||||
    if (size == 0)
 | 
			
		||||
    {
 | 
			
		||||
        return nullptr;
 | 
			
		||||
    }
 | 
			
		||||
    std::random_device rd;
 | 
			
		||||
    std::mt19937 gen(rd());
 | 
			
		||||
    std::uniform_int_distribution<int> dis(1, 100);
 | 
			
		||||
 | 
			
		||||
    std::queue<int> Q;
 | 
			
		||||
    std::queue<TreeNode *> T;
 | 
			
		||||
    for (int i = size; i > 0; --i)
 | 
			
		||||
    {
 | 
			
		||||
        Q.push(dis(gen));
 | 
			
		||||
        std::cout << Q.back() << " ";
 | 
			
		||||
    }
 | 
			
		||||
    std::cout << std::endl;
 | 
			
		||||
 | 
			
		||||
    TreeNode *head = new TreeNode(Q.front());
 | 
			
		||||
    T.push(head);
 | 
			
		||||
    Q.pop();
 | 
			
		||||
 | 
			
		||||
    while (!Q.empty())
 | 
			
		||||
    {
 | 
			
		||||
        if (T.front()->left != nullptr && T.front()->right != nullptr)
 | 
			
		||||
        {
 | 
			
		||||
            T.pop();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        TreeNode *temp = new TreeNode(Q.front());
 | 
			
		||||
        Q.pop();
 | 
			
		||||
 | 
			
		||||
        if (T.front()->left == nullptr)
 | 
			
		||||
        {
 | 
			
		||||
            T.front()->left = temp;
 | 
			
		||||
            T.push(temp);
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (T.front()->right == nullptr)
 | 
			
		||||
        {
 | 
			
		||||
            T.front()->right = temp;
 | 
			
		||||
            T.push(temp);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return head;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void lookTree(TreeNode *root)
 | 
			
		||||
{
 | 
			
		||||
    std::queue<TreeNode *> q;
 | 
			
		||||
    q.push(root);
 | 
			
		||||
    while (!q.empty())
 | 
			
		||||
    {
 | 
			
		||||
        TreeNode *head = q.front();
 | 
			
		||||
        q.pop();
 | 
			
		||||
        std::cout << head->val << " ";
 | 
			
		||||
        if (head->left)
 | 
			
		||||
        {
 | 
			
		||||
            q.push(head->left);
 | 
			
		||||
        }
 | 
			
		||||
        if (head->right)
 | 
			
		||||
        {
 | 
			
		||||
            q.push(head->right);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // LEECODE_C_TREE_H
 | 
			
		||||
							
								
								
									
										30
									
								
								else/symmetry.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								else/symmetry.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/4.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_SYMMETRY_H
 | 
			
		||||
#define LEECODE_C_SYMMETRY_H
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <stack>
 | 
			
		||||
 | 
			
		||||
bool isSymmetry(std::string target) {
 | 
			
		||||
    std::stack<char> tk;
 | 
			
		||||
    for (int i = 0; i < target.length(); ++i) {
 | 
			
		||||
        if (i < target.length() / 2) {
 | 
			
		||||
            tk.push(target[i]);
 | 
			
		||||
        }
 | 
			
		||||
        if (i == target.length() / 2 && target.length() % 2 == 1) {
 | 
			
		||||
            i++;
 | 
			
		||||
        }
 | 
			
		||||
        if (i >= target.length() / 2) {
 | 
			
		||||
            if (target[i] != tk.top()) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            tk.pop();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_SYMMETRY_H
 | 
			
		||||
							
								
								
									
										66
									
								
								else/test1.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								else/test1.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/12/17.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_TEST1_H
 | 
			
		||||
#define LEECODE_C_TEST1_H
 | 
			
		||||
 | 
			
		||||
#include "stdlib.h"
 | 
			
		||||
 | 
			
		||||
typedef struct LinkNode {
 | 
			
		||||
    char value;
 | 
			
		||||
    struct LinkNode *next;
 | 
			
		||||
} *Node;
 | 
			
		||||
 | 
			
		||||
struct p {
 | 
			
		||||
    int length;
 | 
			
		||||
    Node head;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
p create(Node l1) {
 | 
			
		||||
    if (!l1) {
 | 
			
		||||
        return p{0, nullptr};   //NULL
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Node temp = (Node) malloc(sizeof(LinkNode));
 | 
			
		||||
    temp->value = l1->value;
 | 
			
		||||
    l1 = l1->next;
 | 
			
		||||
 | 
			
		||||
    Node head = temp;
 | 
			
		||||
 | 
			
		||||
    int length = 1;
 | 
			
		||||
    while (l1) {
 | 
			
		||||
        temp = (Node) malloc(sizeof(LinkNode));
 | 
			
		||||
        temp->value = l1->value;
 | 
			
		||||
        temp->next = head;
 | 
			
		||||
        l1 = l1->next;
 | 
			
		||||
        head = temp;
 | 
			
		||||
        length++;
 | 
			
		||||
    }
 | 
			
		||||
    return p{length, head};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Node test(Node l1, Node l2) {
 | 
			
		||||
    p p1 = create(l1);
 | 
			
		||||
    p p2 = create(l2);
 | 
			
		||||
 | 
			
		||||
    Node n1 = p1.head;
 | 
			
		||||
    Node n2 = p2.head;
 | 
			
		||||
 | 
			
		||||
    int index = 0;
 | 
			
		||||
    while (n1->value == n2->value) {
 | 
			
		||||
        n1 = n1->next;
 | 
			
		||||
        n2 = n2->next;
 | 
			
		||||
        index++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Node ps = l1;
 | 
			
		||||
    int length = p1.length - index;
 | 
			
		||||
    while (length--) {
 | 
			
		||||
        ps = ps->next;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ps;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_TEST1_H
 | 
			
		||||
							
								
								
									
										76
									
								
								else/topic/2017.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								else/topic/2017.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,76 @@
 | 
			
		||||
#include "../structs/Tree.h"
 | 
			
		||||
#include <queue>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void reversechild(TreeNode *root)
 | 
			
		||||
{
 | 
			
		||||
    TreeNode *head = root;
 | 
			
		||||
    queue<TreeNode *> q;
 | 
			
		||||
    q.push(head);
 | 
			
		||||
    while (!q.empty())
 | 
			
		||||
    {
 | 
			
		||||
        head = q.front();
 | 
			
		||||
        q.pop();
 | 
			
		||||
        if (head->left)
 | 
			
		||||
        {
 | 
			
		||||
            q.push(head->left);
 | 
			
		||||
        }
 | 
			
		||||
        if (head->right)
 | 
			
		||||
        {
 | 
			
		||||
            q.push(head->right);
 | 
			
		||||
        }
 | 
			
		||||
        swap(head->left, head->right);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include <unordered_map>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
unordered_map<int, int> statistic(vector<int> L1)
 | 
			
		||||
{
 | 
			
		||||
    unordered_map<int, int> m;
 | 
			
		||||
    for (auto i : L1)
 | 
			
		||||
    {
 | 
			
		||||
        m[i] = m[i] + 1;
 | 
			
		||||
    }
 | 
			
		||||
    return m;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "../structs/List.h"
 | 
			
		||||
 | 
			
		||||
ListNode *integrateList(LitNode *L1, ListNode *l2)
 | 
			
		||||
{
 | 
			
		||||
    ListNode *head = L1->value < L2->value ? L1 : L2;
 | 
			
		||||
    if (L1->value < L2->value)
 | 
			
		||||
    {
 | 
			
		||||
        L1 = L1->next;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        L2 = L2->next;
 | 
			
		||||
    }
 | 
			
		||||
    ListNode *temp = head->next;
 | 
			
		||||
    while (L1 && L2)
 | 
			
		||||
    {
 | 
			
		||||
        temp->next = L1->value < L2->value ? L1 : L2;
 | 
			
		||||
        if (L1->value < L2->value)
 | 
			
		||||
        {
 | 
			
		||||
            L1 = L1->next;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            L2 = L2->next;
 | 
			
		||||
        }
 | 
			
		||||
        temp = temp->next;
 | 
			
		||||
    }
 | 
			
		||||
    if(L1){
 | 
			
		||||
        temp->next = L1;
 | 
			
		||||
    }
 | 
			
		||||
    if(L2){
 | 
			
		||||
        temp->next = L2;
 | 
			
		||||
    }
 | 
			
		||||
    return head;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								else/topic/2019.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								else/topic/2019.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void odd_even(vector<int> &array)
 | 
			
		||||
{
 | 
			
		||||
    int left = 0;
 | 
			
		||||
    int right = array.size() - 1;
 | 
			
		||||
    while (left < right)
 | 
			
		||||
    {
 | 
			
		||||
        while (left < right && array[right] % 2 == 0)
 | 
			
		||||
        {
 | 
			
		||||
            right--;
 | 
			
		||||
        }
 | 
			
		||||
        while (left < right && array[left] % 2 == 1)
 | 
			
		||||
        {
 | 
			
		||||
            left++;
 | 
			
		||||
        }
 | 
			
		||||
        swap(array[left], array[right]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "../structs/Tree.h"
 | 
			
		||||
 | 
			
		||||
void insertSearchTree(TreeNode *root, int value)
 | 
			
		||||
{
 | 
			
		||||
    TreeNode *dist = new TreeNode();
 | 
			
		||||
    dist->val = value;
 | 
			
		||||
    if (!root)
 | 
			
		||||
    {
 | 
			
		||||
        root = dist;
 | 
			
		||||
    }
 | 
			
		||||
    TreeNode *temp = root;
 | 
			
		||||
    while (temp)
 | 
			
		||||
    {
 | 
			
		||||
        if (temp->val > value)
 | 
			
		||||
        {
 | 
			
		||||
            if (temp->left)
 | 
			
		||||
            {
 | 
			
		||||
                temp = temp->left;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            temp->left = dist;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        if (temp->val < value)
 | 
			
		||||
        {
 | 
			
		||||
            if (temp->right)
 | 
			
		||||
            {
 | 
			
		||||
                temp = temp->right;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            temp->left = dist;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        if (temp->val == value)
 | 
			
		||||
        {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								else/topic/2020.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								else/topic/2020.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
#include "../structs/Tree.h"
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
TreeNode *getX(TreeNode *root, int x)
 | 
			
		||||
{
 | 
			
		||||
    TreeNode *temp = root;
 | 
			
		||||
    while (temp)
 | 
			
		||||
    {
 | 
			
		||||
        if (temp->val > x)
 | 
			
		||||
        {
 | 
			
		||||
            if (temp->left)
 | 
			
		||||
            {
 | 
			
		||||
                temp = temp->left;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            if (temp->right)
 | 
			
		||||
            {
 | 
			
		||||
                temp = temp->right;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return temp;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								else/topic/2021.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								else/topic/2021.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <stack>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
int test1(vector<int> array)
 | 
			
		||||
{
 | 
			
		||||
    if (array.empty())
 | 
			
		||||
    {
 | 
			
		||||
        return INT_MIN;
 | 
			
		||||
    }
 | 
			
		||||
    stack<int> S;
 | 
			
		||||
    int norm = array[0];
 | 
			
		||||
    int m = INT_MIN;
 | 
			
		||||
    S.push(array[0]);
 | 
			
		||||
    for (int i = 1; i < array.size(); i++)
 | 
			
		||||
    {
 | 
			
		||||
        m = max(m, norm - array[i]);
 | 
			
		||||
        while (!S.empty() && array[i] > S.top())
 | 
			
		||||
        {
 | 
			
		||||
            S.pop();
 | 
			
		||||
        }
 | 
			
		||||
        if (S.empty())
 | 
			
		||||
        {
 | 
			
		||||
            norm = array[i];
 | 
			
		||||
        }
 | 
			
		||||
        S.push(array[i]);
 | 
			
		||||
    }
 | 
			
		||||
    return m;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "../structs/Tree.h"
 | 
			
		||||
int test2(TreeNode *root){
 | 
			
		||||
    //先镜像,再按需遍历
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										66
									
								
								else/topic/2022.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								else/topic/2022.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
#include "../structs/List.h"
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
vector<vector<int>> test1(ListNode *head)
 | 
			
		||||
{
 | 
			
		||||
    vector<vector<int>> ret;
 | 
			
		||||
 | 
			
		||||
    int prev = head->value;
 | 
			
		||||
    int index = 0;
 | 
			
		||||
    ret.push_back(vector<int>(1, prev));
 | 
			
		||||
 | 
			
		||||
    ListNode *temp = head->next;
 | 
			
		||||
 | 
			
		||||
    while (!temp)
 | 
			
		||||
    {
 | 
			
		||||
        if (prev == temp->value)
 | 
			
		||||
        {
 | 
			
		||||
            index++;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            index = 0;
 | 
			
		||||
        }
 | 
			
		||||
        if (index >= ret.size())
 | 
			
		||||
        {
 | 
			
		||||
            ret.push_back(vector<int>(1, temp->value));
 | 
			
		||||
        }
 | 
			
		||||
        if (index < ret.size())
 | 
			
		||||
        {
 | 
			
		||||
            ret[index].push_back(temp->value);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        temp = temp->next;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "../structs/Tree.h"
 | 
			
		||||
 | 
			
		||||
pair<bool, int> help(TreeNode *root)
 | 
			
		||||
{
 | 
			
		||||
    if (!root)
 | 
			
		||||
    {
 | 
			
		||||
        return {true, 0};
 | 
			
		||||
    }
 | 
			
		||||
    auto p1 = help(root->left);
 | 
			
		||||
    auto p2 = help(root->right);
 | 
			
		||||
    if (!p1.first || !p2.first)
 | 
			
		||||
    {
 | 
			
		||||
        return {false, 0};
 | 
			
		||||
    }
 | 
			
		||||
    if (abs(p1.second - p2.second) > 1)
 | 
			
		||||
    {
 | 
			
		||||
        return {false, 0};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return {true, max(p1.second, p2.second) + 1};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool test2(TreeNode *root)
 | 
			
		||||
{
 | 
			
		||||
    return help(root).first;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								else/topic/2023.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								else/topic/2023.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include<stack>
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
bool test1(vector<int> array){
 | 
			
		||||
    stack<int> S;
 | 
			
		||||
    for (int i = 0; i < array.size()/2; i++)
 | 
			
		||||
    {
 | 
			
		||||
        S.push(array[i]);
 | 
			
		||||
    }
 | 
			
		||||
    int tag = array.size()%2;
 | 
			
		||||
    for(int i = array.size()/2 + tag;i<array.size();i++){
 | 
			
		||||
        if (S.top() != array[i])
 | 
			
		||||
        {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        S.pop();
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										58
									
								
								else/topic/2024.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								else/topic/2024.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/12/21.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_2024_H
 | 
			
		||||
#define LEECODE_C_2024_H
 | 
			
		||||
 | 
			
		||||
#include "stack"
 | 
			
		||||
 | 
			
		||||
struct Node {
 | 
			
		||||
    int value;
 | 
			
		||||
    struct Node *left;
 | 
			
		||||
    struct Node *right;
 | 
			
		||||
    bool tag1;
 | 
			
		||||
    bool tag2;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
int calculate(stack<Node *> S) {
 | 
			
		||||
    stack<Node *> temp;
 | 
			
		||||
    int num = 0;
 | 
			
		||||
    int i = 0;
 | 
			
		||||
    while (!S.empty()) {
 | 
			
		||||
        num += S.top()->value * pow(10, i++);
 | 
			
		||||
        S.pop();
 | 
			
		||||
    }
 | 
			
		||||
    return num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int get(Node *root) {
 | 
			
		||||
    if (!root) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    int count = 0;
 | 
			
		||||
    stack<Node *> S;
 | 
			
		||||
    S.push(root);
 | 
			
		||||
    while (!S.empty()) {
 | 
			
		||||
        if (!S.top()->left && !S.top()->right) {
 | 
			
		||||
            count += calculate(S);
 | 
			
		||||
        }
 | 
			
		||||
        if (S.top()->tag1 == 0 && S.top()->left) {
 | 
			
		||||
            S.top()->tag1 = 1;
 | 
			
		||||
            S.push(S.top()->left);
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        if (S.top()->tag2 == 0 && S.top()->left) {
 | 
			
		||||
            S.top()->tag2 = 1;
 | 
			
		||||
            S.push(S.top()->right);
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        S.pop();
 | 
			
		||||
    }
 | 
			
		||||
    return count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_2024_H
 | 
			
		||||
							
								
								
									
										138
									
								
								else/traverse.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										138
									
								
								else/traverse.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,138 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by 李洋 on 2023/10/12.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef LEECODE_C_TRAVERSE_H
 | 
			
		||||
#define LEECODE_C_TRAVERSE_H
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
#include "structs/Tree.h"
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <stack>
 | 
			
		||||
 | 
			
		||||
queue<TreeNode *> Q;
 | 
			
		||||
 | 
			
		||||
// 递归写法 - int型
 | 
			
		||||
// 先序遍历 preorder traversal
 | 
			
		||||
void preorder_recursion_help(TreeNode *head) {
 | 
			
		||||
    if (head != nullptr) {
 | 
			
		||||
        Q.push(head);
 | 
			
		||||
        preorder_recursion_help(head->left);
 | 
			
		||||
        preorder_recursion_help(head->right);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
queue<TreeNode *> preorder_recursion(TreeNode *head) {
 | 
			
		||||
    preorder_recursion_help(head);
 | 
			
		||||
    return Q;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// 中序遍历 inorder traversal
 | 
			
		||||
void inorder_recursion_help(TreeNode *head) {
 | 
			
		||||
    if (head != nullptr) {
 | 
			
		||||
        inorder_recursion_help(head->left);
 | 
			
		||||
        Q.push(head);
 | 
			
		||||
        inorder_recursion_help(head->right);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
queue<TreeNode *> inorder_recursion(TreeNode *head) {
 | 
			
		||||
    inorder_recursion_help(head);
 | 
			
		||||
    return Q;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// 后序遍历 postorder traversal
 | 
			
		||||
void postorder_recursion_help(TreeNode *head) {
 | 
			
		||||
    if (head != nullptr) {
 | 
			
		||||
        postorder_recursion_help(head->left);
 | 
			
		||||
        postorder_recursion_help(head->right);
 | 
			
		||||
        Q.push(head);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
queue<TreeNode *> postorder_recursion(TreeNode *head) {
 | 
			
		||||
    postorder_recursion_help(head);
 | 
			
		||||
    return Q;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// 非递归写法
 | 
			
		||||
// 先序遍历 preorder traversal
 | 
			
		||||
queue<TreeNode *> preorder(TreeNode *head) {
 | 
			
		||||
    if (!head) {
 | 
			
		||||
        return Q;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto temp = head;
 | 
			
		||||
 | 
			
		||||
    stack < TreeNode * > S;
 | 
			
		||||
 | 
			
		||||
    S.push(temp);
 | 
			
		||||
 | 
			
		||||
    TreeNode *t;
 | 
			
		||||
 | 
			
		||||
    while (!S.empty()) {
 | 
			
		||||
        t = S.top();
 | 
			
		||||
        S.pop();
 | 
			
		||||
        Q.push(t);
 | 
			
		||||
        if (t->right)
 | 
			
		||||
            S.push(t->right);
 | 
			
		||||
        if (t->left)
 | 
			
		||||
            S.push(t->left);
 | 
			
		||||
    }
 | 
			
		||||
    return Q;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 中序遍历 inorder traversal
 | 
			
		||||
queue<TreeNode *> inorder(TreeNode *head) {
 | 
			
		||||
    auto temp = head;
 | 
			
		||||
    stack < TreeNode * > S;
 | 
			
		||||
 | 
			
		||||
    while (temp != nullptr || !S.empty()) {
 | 
			
		||||
        if (temp) {
 | 
			
		||||
            S.push(temp);
 | 
			
		||||
            temp = temp->left;
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        temp = S.top();
 | 
			
		||||
        Q.push(temp);
 | 
			
		||||
        S.pop();
 | 
			
		||||
        temp = temp->right;
 | 
			
		||||
    }
 | 
			
		||||
    return Q;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 后序遍历 postorder traversal
 | 
			
		||||
queue<TreeNode *> postorder(TreeNode *head) {
 | 
			
		||||
    auto temp = head;
 | 
			
		||||
    stack <pair<TreeNode *, bool>> S;
 | 
			
		||||
 | 
			
		||||
    while (temp != nullptr || !S.empty()) {
 | 
			
		||||
        if (temp) {
 | 
			
		||||
            S.emplace(temp, false);
 | 
			
		||||
            temp = temp->left;
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        temp = S.top().first;
 | 
			
		||||
 | 
			
		||||
        if (temp->right == nullptr || S.top().second) {
 | 
			
		||||
            Q.push(temp);
 | 
			
		||||
            S.pop();
 | 
			
		||||
            temp = nullptr;
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        S.top().second = true;
 | 
			
		||||
        temp = temp->right;
 | 
			
		||||
    }
 | 
			
		||||
    return Q;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test1() {
 | 
			
		||||
    auto TreeNode = createRandomTree(10);
 | 
			
		||||
    auto Q = postorder(TreeNode);
 | 
			
		||||
    while (!Q.empty()) {
 | 
			
		||||
        cout << Q.front()->val << " ";
 | 
			
		||||
        Q.pop();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //LEECODE_C_TRAVERSE_H
 | 
			
		||||
		Reference in New Issue
	
	Block a user