old

//
// Created by apple on 2020/3/12.
//

#include <iostream>

class Test{


};
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

int getlength(ListNode* listnode){
    int count =0;
    ListNode* temp = listnode;
    while(temp!=NULL){
        count ++;
        temp = temp->next;
    }
}

Listnode * getcommon(ListNode* list1, ListNode* list2){

    int length1 = getlength(list1);
    int length2 = getlength(list2);

    if(length1>length2){
        ListNode* longlist = list1;
        ListNode* shortlist = list2;
        int diff = length1 - length2;
    } else{
        ListNode* longlist = list2;
        ListNode* shortlist = list1;
        int diff = length2 - length1;
    }

    for (int i=0;i<diff;i++){
        longlist = longlist->next;
    }

    while (longlist->next!= NULL && shortlist!= NULL && (longlist != shortlist)){
        longlist = longlist->next;
        shortlist = shortlist->next;
    }
    return longlist;
}

int treedepth(binarytree *tree){

    if(tree==NULL){
        return  0;
    }
    int left = treedepth(tree->left);
    int right = treedepth(tree->right);
    return (left> right) ? left++: right++;
}

//冒泡排序核心
for (int i=0; i<n; i++){
for (int j =0; j<n -1;j++){
if (array[j]>array[j+1]){
swap(array[j],array[j+1]);
}
}
}
//选择排序 核心,选择排序每次都选一个最大的出来
for(int i =0; i<n; i++){
for (int j =i,j<n-1;j++){
if (array[j]>array[j+1]){
max = array[j];
}
}
}
//quick sort

void quicksort (int array[], int low, int high){
    if(low<high){
        int index = getindex(array,low,high);
        quicksort(array,index+1,high);
        quicksort(array,0,high);
    }
}

int getindex(int array[],int low, int high){
    int temp = array[low];
    while (low<high){
        if(array[high] >temp ){
            high --;
        }
        array[low] = array[high];
        if(array[low]<temp){
            low++;
        }
        array[high] = array[low];
    }
    array[low] =temp;
    return low;
}

//非递归的形式来实现快排

void quichsortnorecusive(int array[],int low,int high[]){
    stack<int> s;

    s.push(low);
    s.push(high);

    while(!s.empty()){
        int h = s.top(); s.pop();
        int l = s.top(); s.pop();
        int index= getindex(array,l,h);
        if(index-1>l){
            s.push(l);//左边
            s.push(index-1);//右边
        }
        else(index+1<h){
            s.push(index +1);
            s.push(h);
        }

    }

}


//归并排序
void sort(int array[],int low,int high){
    if(low<high){
        int mid = (low+high)/2;
        sort(array,low,mid);
        sort(array,mid+1,high);
        merge(array,low,mid,high);
    }
}

void merge(int array[],int low,int mid,int high){
    int temp[low + high - 1];
    if (low<high){
        int p1 = low;
        int p2 = mid +1;
        int i =0;
        while (p1<mid && p2<high){
            temp[i++] = array[p1] > array[p2] ? array[p1++] : array[p2++];
        }
        while (p1<mid){
            temp[i++] = array[p1++];
        }
        while (p2<high){
            temp[i++] = array[p2++];
        }
    }
    for (int i =0; i< low + high - 1; i++){
        array[low+i] = temp[i];
    }
}


void insertSort(int a[],  int length) {
    for (int i = 1; i < length; i++) {
        for (int j = i - 1; j >= 0 && a[j + 1] < a[j]; j--) {
            swap(a[j], a[j + 1]);
        }
    }

}


void insertsort(int array[],int length){
    for(int i=1; i<length,i++){
        for (j=i-1; j>0 && array[j+1] <array[j]; j--){
            swap(array[j+1],array[j]);
        }
    }
}


int adjust(vector<int> &array, int len, int index){
    if(len>index){
        return;
    }
    int left = 2 * index +1;
    int right = 2 * index +2;
    int midindex = index;
    if (left<len && array[left]> array[midindex]){
        midindex = left;
    }
    if(right<len && array[right]>array[midindex]){
        midindex =right;
    }

    if (midindex!=index){
        swap(array[index],array[midindex]);
        adjust(array,len,midindex);
    }
}

void heapsort(int array[],int len,int size){
    for (int i = size/2 -1; i>0; i--){
        adjust((array,size,i));
    }
    for (int i = size -1; i>0; i--){
        swap(array[0],array[i]);
        adjust(array,i,0) //在这里将未完成的排序继续进行下去
    }
}

void heap_sort(vector<int> &array, int len, int size){
    //  构建大根堆,从最后一个非叶子节点往上,也就是从最后一个父节点往上开始循环创建
    //  首先我们找到最后一个叶子节点,然后从下向上开始构建

    for (int i = size / 2 -1; i>0; i--){
        adjust (array, size, i);
    }

    for (int i = size - 1; i>=1; i-- ){
        swap(array[0],array[i]); // 在这里将最大的数放到末尾
        adjust(array,i,0) //在这里将未完成的排序继续进行下去
    }
}


void preorder(Tree *tree){
    if(t==NULL){
        return;
    }
    cout<<tree->val<<endl;
    preorder(tree->left);
    preorder(tree->right);
}


void inorder(Tree *tree){
    if(tree==NULL){
        return;
    }
    inorder(tree->left);
    cout<<tree->val;
    inorder(tree->right);
}

void lastorder (Tree *tree){
    if (tree== NULL){
        return;
    }
    lastorder(tree->left);
    lastorder(tree->right);
    cout<<tree->val;
}

void preorder( Tree *tree){
    stack<Tree *> s;
    while(tree != NULL || (!s.empty())){
        if(tree!=NULL){
            cout<<tree->val<<endl;
            s.push(tree);
            tree = tree->left;
        }else{
            tree = s.top();
            s.pop();
            tree = tree->right;
        }
    }
}

void inorder(Tree* tree){
    stack<Tree*>s;
    while(tree!=NULL || !s.empty()){
        if (tree!=NULL){
            s.push(tree);
            tree = tree->left();
        } else{
            tree =s.top();
            s.pop();
            cout<<tree->val;
            tree = tree->right;
        }

    }
}

void lastvisit(Tree *tree){
    stack<int> s;
    Tree * last = root;
    while (tree!=NULL || !s.empty()){
        if(tree!=NULL){
            s.push(tree);
            tree = tree->left;
        } else if( tree->right == NULL || last == tree->right){
            tree = s.top();
            cout<<tree->val<<endl;
            last = tree;
            tree = NULL;
        } else{
            tree = tree->right;
        }
    }
}

// 从上到下打印二叉树

void printtree(Tree * tree){
    if (tree ==NULL){
        return;
    }
    dequeue<Tree*> s;
    s.push_back(tree);
    while(!s.empty()){
        Tree *temp = s.front();
        s.pop_front();
        if(tree->left!=NULL){
            tree = tree->left;
            s.push_back(tree);
        }
        if(tree->right!=NULL){
            tree = tree->right;
            s.push_back(tree);
        }
    }
}

int binarysearch(int array[],int low, int high,int target){
    if(low<=high){
        int mid = (low + high)/2;
        if (array[mid]==target){
            return mid;
        }
        else if (array[mid]>target){
            binarysearch(array,low,mid-1,target);
        }
        else{
            binarysearch(array,mid+1,high,target);
        }
    }
    return  -1;
}

int binarysearch(int array[], int low,int high,int target){
    while (low<=high){
        int mid = (low+high)/2;
        if (array[mid]==target){
            return mid;
        } else if ( array[mid]<target){
            low = mid +1;
        }
        else {
            high = mid -1;
        }
    }
    return -1;
}






#include<iostream>
using namespace std;

class  singleton{
private:
    static singleton * intstance;
    singleton(){};
public:
    static singleton* getinstance (){
        if (intstance == NULL){
            intstance == new singleton();
        }
        return intstance;
    }
};


singleton * singleton::intstance =NULL;

int main(){

    singleton * s1 = singleton::getinstance();// 直接调用 类的静态成员函数
    singleton * s2 = singleton::getinstance();// 对于类的非静态成员函数是///不能调用的

    if (s1==s2){
        cout<<"equal!!!"<<endl;
    }

    return 0;
}

class singleton{
private:
    static singleton * single;
    singleton(){};

public:
    static singleton *  getinstance (){
        if ( single == NULL;){
            single = new singleton();
        }
        return single;
    }
};
singleton * singleton:: single == NULL;

int main(){
    singleton * s1 =  singleton::getinstance();
    singleton * s2 =  singleton::getinstance();
}



void product_thread(){

    while(true){
        std::unique_lock<std::mutex> lck(m_mux);
        num = ++num % 1000;
        data.push_front(num);
        printf("product %d\n", num);
        lck.unlock();
        sleep(2);
    }

    void product_thread(){
        while(true){
            std::unique_lock<std::mutex> lck(mutex);
            num = num++;
            data.push(num);
            lck.unlock;}
    }
}

void comsumer_thread(){
    while(true){
        std::unique<std::mutex> lck(mutex);
        if (data.empty()){
            lck.unlock();
        }
        data.pop();
        lck.unlock();
    }
}

ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    if(pListHead ==NULL){
        return NULL;
    }
    ListNode* node = pListHead;
    int count =0;
    while(node!= NULL){
        ++count;
        node = node->next;
    }

    if(k==0 || k>count){
        return  NULL;
    }
    ListNode* first = pListHead;
    ListNode* second = pListHead;
    for(int i = 0;i<k-1; i++){
        first = first->next;
    }
    while ( first->next !=NULL){
        first = first->next;
        second = second->next;
    }
    return  second;
}

ListNode* ReverseList(ListNode* pHead) {
    if(pHead ==NULL){
        return NULL;
    }
    ListNode* cur = pHead;
    ListNode* pre = NULL;
    ListNode* temp = NULL;
    while(cur!=NULL){
        temp = cur->next;// 暂存一下下一个
        cur->next = pre;//当前值志向前一个
        pre = cur;//前一个值向后一步
        cur = temp;//把下一个进行后移
    }
}

ListNode* ReverseList(ListNode* pHead) {
    if (pHead == NULL){
        return NULL;
    }
    ListNode *pre =NULL;
    ListNode *cur = pHead;
    ListNode * temp=NULL;

    while (cur!=NULL){
        temp = cur->next;
        cur->next = pre;
        pre = cur;
        cur= temp;
    }
}




#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

char a[1001], b[1001];
int dp[1001][1001], len1, len2;

void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else if(dp[i-1][j] > dp[i][j-1])
                dp[i][j] = dp[i-1][j];
            else
                dp[i][j] = dp[i][j-1];
        }
    }
}

int main()
{
    while(~scanf(" %s",a))
    {
        scanf(" %s", b);
        memset(dp, 0, sizeof(dp));
        len1 = strlen(a);
        len2 = strlen(b);
        lcs(len1, len2);
        printf("%d\n", dp[len1][len2]);
    }
    return 0;
}




###    遍历数组,遍历到i时,a0,a1...ai-1是已经排好序的,然后从i到n选择出最小的,记录下位置,如果不是第i个,则和第i个元素交换。此时第i个元素可能会排到相等元素之后,造成排序的不稳定。


void select_sort(int array[],int n){
    int i, position, j, temp;
    for (i = 1; i++;;i<n){
        pos = i;

        for (j = i+1; j<n;j++){

            if (array[pos] > array[j]){ 如果 i 当前的值 比j 的值大的话,就记录一下 pos 的位置
                pos = j;
            }
            if (pos !=i){

                temp = array[i];
                array[i] = array[pos];
                array[pos] = temp;

            }

        }

    }

}



### 冒泡排序算法

void bumble_sort(int array[],int n){
    if (array = null ||n<2){
        return ;
    }
    for (int i=0; i <n; i++){
        for (int j= 0; j<n-i-1; j++){
            if (array[j]> array[j+1]){
                int temp = array[j+1]
                array[j+1] = array[j];
                array[j] = temp;
            }
        }
    }

}

改进版本的 冒泡排序,记录一下交换位置的地方
        优化二:某一轮结束位置为j,但是这一轮的最后一次交换发生在lastSwap的位置,则lastSwap到j之间是排好序的,下一轮的结束点就不必是j--了,而直接到lastSwap即可,代码如下:


void bubble_sort (int array[],int n){
    if (array = null || n<2){
        return ;

    }
    int lastswap = 0;
    for (int j = n-1; j >=0; j= lastswap){
        lastswap = 0;

        for (int i =0; i <j; i++){
            if (array[i]>array[i+1]){
                int temp = array[i+1];
                array[i+1] = array[i];
                array[i] = temp;
                lastswap = i;
            }
        }
    }
}


  ①先从队尾开始向前扫描且当low < high时,如果a[high] > tmp,则high–,但如果a[high] < tmp,则将high的值赋值给low,即arr[low] = a[high],同时要转换数组扫描的方式,即需要从队首开始向队尾进行扫描了
  ②同理,当从队首开始向队尾进行扫描时,如果a[low] < tmp,则low++,但如果a[low] > tmp了,则就需要将low位置的值赋值给high位置,即arr[low] = arr[high],同时将数组扫描方式换为由队尾向队首进行扫描.
  ③不断重复①和②,知道low>=high时(其实是low=high),low或high的位置就是该基准数据在数组中的正确索引位置.

## quick sort

int getindex(int array[],int low, int high){
    if (array =null,array.length <2){
        return;
    }
    int temp = array[low];

    while (low<high){

        while (low<high && array[high]>tmp){
            high --;
        }// 两个指针,如果后面的数比temp 大的话, high --;

        array[low] = array[high]; //交换 两个值

        while(low<high && array[low]<tmp){
            low ++; // 如果 array[low] 比 temp 小的话 low++;
        }
        array[high] = array[low]; //交换 两个值
    }
    array[low] = temp; //最后停止时low 或者high 的值为 temp 的位置 ,此时左边的数据比他小,右边的数据比他大。
    return low; //返回 位置。
}

void quick_sort(int array[], int low,int high){
    if (low< high){
        int index = getindex(array, low, high);
        quick_sort ( array,index+1,high);
        quick_sort ( array, 0, index-1);
    }
}

reference:  https://blog.csdn.net/nrsc272420199/article/details/82587933


https://blog.csdn.net/ccblogger/article/details/82384480


### heap sort
// 采用递归来在这里进行实现

void adjust (vector<int> &array, int len, int index){
    if (index > len){ //在这里判断边界值
        return;
    }
    int left = 2 * index + 1; //index 的左节点
    int right = 2 * index + 2;// index 的右节点
    int midindex = index;
    if (left < len && array[left] > array[index]){midindex = left;}
    if (right < len && array[right] > array[index]){midindex = right;}
    if (midindex != index){
        swap(array[midindex],array[index]);//交换一下顺序的值
        adjust(array, len, midindex); //递归调用,在这里调用递归的原因是如果不是调整最后一个父节点的话
        //会出现调整之后的父节点的值比原来他的儿子们的数据要小
    }

}

void heap_sort(vector<int> &array, int len, int size){
    //  构建大根堆,从最后一个非叶子节点往上,也就是从最后一个父节点往上开始循环创建
    //  首先我们找到最后一个叶子节点,然后从下向上开始构建

    for (int i = size / 2 -1; i>0; i--){
        adjust (array, size, i);
    }

    for (int i = size - 1; i>=1; i-- ){
        swap(array[0],array[i]); // 在这里将最大的数放到末尾
        adjust(array,i,0) //在这里将未完成的排序继续进行下去
    }

}

https://blog.csdn.net/baidu_31437863/article/details/95043086



### C ++ stl heap_sort

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void my_function(int i){
    cout<< " " << i;
}
int main(){
    vector<int> v;
    v.push_back(3);
    v.push_back(9);
    v.pash_back(3);
    v.push_back(12);
    v.push_back(17);
    v.push_back(20);
    for_each (v.begin(),v.end(),my_function); // output the value of v
    cout<<endl;
    make_heap(v.begin(),v.end()); // create the heap
    sort_heap(v.begin(),v.end()); // sort the heap
    for_each(v.begin(),v.end(),my_function); // 默认排序大顶堆, 最后如果倒序输出的话就是小顶堆了
    cout<<end;
}


### 递归算法来计算 阶乘:
1、递归阶乘n! = n * (n-1) * (n-2) * ...* 1(n>0)

int recursion_multiply(int n){
    if (n==1){
        return 1;
    }
    return n * recursion_multiply(n-1);
}

2: 斐波那契数列

long feibo(int n){
    if (n ==0){
        return 0;
    }
    if(n==1){
        return 1;
    }
    if (n>1){
        return feibo(n-1) + feibo(n-2);
    }
}
3: 对一系列数据进行求和 a1 + a2 + a3 + a4 + ... + an

#include<iostream>
using namespace std;
int main(){

}


class A{
    virtual void foo();
}

class B: public A{
    void foo();//ok
    virtual foo();//ok
    void foo() override; //ok
    virtual void  foo();//ok
    virtual void fo0() override; //error
}


class base {
    virtual foo ();
}

class A:base(){
void foo () final;
void bar() final;

}

class B final A{
        void foo() override;//error
}

class C: B{
    //error B is final
}


#include<iostream>
using namespace std;

inline char * dbtest(int a){
    return (i%1)? "odd":"even"
}
int main(){
    for(int i=0; i<100; i++){
        print ("i:% d %s",i,detest());
    }
}

https://www.cnblogs.com/inception6-lxc/p/8686156.html



int treedepth (TreeNode root){
    if(root != NULL){
        int left = treedepth(root.left);

        int roght = treedepth(root.right);

        return left >right ? left +1 : right +1;
    }
    else {
        return 0;
    }
}


### 平衡二叉树

bool isbalanced (TreeNode root){
    if (root == NLL){
        return true;
    }
    int left = treedepth (root.left);
    int right = treedepth (root.right);
    int diff   = left - right;

    if (diff > 1 || diff < -1){
        return false;
    }

    return isbalanced (root.left) && isbalanced (root.right);

}



二、例子程序
        这是一个前辈写的,非常详细
//main.cpp
int   a   =   0;   全局初始化区
char   *p1;   全局未初始化区
main()
{
    int   b;   栈
    char   s[]   =   "abc";   栈
    char   *p2;   栈
    char   *p3   =   "123456";   123456/0在常量区,p3在栈上。
    static   int   c   =0;   全局(静态)初始化区
            p1   =   (char   *)malloc(10);
    p2   =   (char   *)malloc(20);
    分配得来得10和20字节的区域就在堆区。
    strcpy(p1,   "123456");   123456/0放在常量区,编译器可能会将它与p3所指向的"123456"
    优化成一个地方。
}



计算求解链表倒数 k 个节点。

通过使用两个 pointer
ListNode * find_last_k (ListNode * list, unsigned int k){

    if(list == NULL || k ==0 ||list->length <k){
        return NULL;
    }

    ListNode *pfirst = list;
    ListNode *psecond = list;

    for( int i = 0; i <k; i++){
        if (pfirst->next != NUll){
            pfirst = pfirst->next;
        }
    }

    while (pfirst->next != NULL){
        pfirst = pfirst->next;
        psecond = psecond->next;

    }

    return psecond;
}


ListNode * find_last_k (ListNode * list , unsigned int k){

    if (list == NULL  || K ==0 || list->length <k) {
        return NUll;
    }

    ListNode *first = list; // 在这里先创建两个指针
    ListNode *second = list;

    for (int i =0; i<k; i++){
        first = first->next;// 第一个指针先走 k-1 步
    }

    while (first->next != NUll){

        first = first->next;

        second = second->next;
    }
    //最后由于两个指针之间相隔 k-1 个数据, 所以最后的话当第一个指针走到了最后的话,第二个指针正好在 k 个的位置上。

    return second ;


}


# 二维数组查找
bool findnumber (int value, vector<vector<int>> vec){
    if (vec.empty()){
        return false;
    }
    int row = 0;

    int col = vec[0].size() -1 ; //在这里剑 1 是因为数组下坐标从零开始

    while (row < vec.size() && col >0){

        if (vec[row][col] ==value){
            return true;
        }
        else if (vec[row][col] > value){
            col --;
        }
        else {
            row ++;
        }
    }
}

#空格字符串替换

class solution:
        def replacespace(self, s)
return s.replace (' ','%20')

### 单链表操作


struct listnode {
    int vaule;
    listnode * pnext;
};

// add node to the list

void addnode (ListNode ** list , int value){

    ListNode * newnode = new ListNode();
    newnode->value = value;
    newnode->next  = NULL;

    if (*list == NULL){
        *list = newnode;
    } else {
        ListNode * last = *list;
        while(last->next != NULL){
            last = last->next;
        }
        last->next = newnode;
    }
}

void deletenode(ListNode **list, int value){
    if (list ==NULL || *list = NULL){
        return;
    }

    ListNode * del = NULL;

    if (*list->value ==value){
        del = *list;
        *list = *list->next;
    }
    else {
        ListNode * node = *list;
        while (node ->next !=NULL && node->next->value != value){
            node = node->next;//如果没找到就一直向下寻找
        }
        if (node->next !=NULL && node->next->value ==value){
            node = node->next->next; //直接跳过要删除的结
            del = node ->next;
        }
        if(del != NULL){
            delete del;
            del = NULL;
        }
    }
}



void inverselist (ListNode * listnode){
    if (listnode == NULL){
        return;
    }
    ListNode *node = listnode;
    if (node !=NULL){
        if (node->next!= NULL){
            inverselist(node->next);
        }
        cout<< listnode->value <<endl;
    }
}

void reverselist (ListNode *list){
    if (list != NULL){
        return ;
    }

    stack <ListNode*> sta;
    ListNode * temp = list;

    while (temp->next != NULL){
        sta.push(temp);
        temp = temp->next;
    }


    while (temp.size()){
        temp = sta.top();
        cout<< temp->value <<" "<<endl;
        temp.pop();
    }
}


struct binarytree{
    int value;

    binarytree *left;
    binarytree *right;

};


class point {

public:
    void init(){

    }

    static void output(){

    }

};

void main(){
    point::init();// error
    point::output();//ok
}

class point {
public:
    void init(){

    }

    static  void output(){

    }

}


void main(){
    point p1;
    p1.init();
    p1.output();
}



#include<iostream>
using namespace std;



class point {
public:
    void init(){

    }
    static void output(){
        cout<<max<<endl;
    }
private:
    int max;

};

int main(){
    point pt;
    pt.output();//error

} // 因为静态成员函数属于某个类,在在类进行实例话之前就已经进行了初始化,因此是错误的,而非静态成员必须在类实例话之后才进行分配 ,因此在这里调用是错误的

class point {

public:
    void init(){
        output();
    }
    static void output(){

    }
};

int main(){
    point pt;
    pt.init();// ok  非静态成员函数可以调用静态成员函数
}

#include<iostream>
using namespace std;

class Point {
public:

    Point (){
        max ++;
    }
    ~Point(){
        max --;
    }
    static  void output(){

        cout<< max<<endl;
    }

private :
    static int max;

};
int Point:: max =0; //在这里必须要必须进行静态成员变量的初始化

int main (){

    Point pt;
    pt.output();
}

关于 static  讲的特别好的文章:
https://www.runoob.com/w3cnote/cpp-static-usage.html



#include <iostream>
using namespace std;

class Singleton {

private:

    static Singleton *intstance;//静态变量必须在类外进行访问
    Singleton(){};

public:
    static Singleton * getinstance(){
        if (intstance == NULL){
            intstance  =  new Singleton();
        }
        return intstance;
    }
};

Singleton * Singleton::intstance ==NULL;

int main(){
    Singleton *s1 = new Singleton();
    Singleton *s2 = new Singleton();

    if(s1 == s2){

        cout<<"the same !!!"<<endl;
    }

}


#include<iostream>
using namespace std;

class  singleton{
private:
    static singleton * intstance;
    singleton(){};
public:
    static singleton* getinstance (){
        if (intstance == NULL){
            intstance == new singleton();
        }
        return intstance;
    }


};


singleton * singleton::intstance =NULL;

int main(){

    singleton * s1 = singleton::getinstance();// 直接调用 类的静态成员函数
    singleton * s2 = singleton::getinstance();// 对于类的非静态成员函数是///不能调用的

    if (s1==s2){
        cout<<"equal!!!"<<endl;
    }

    return 0;
}




int  Partation (int array[], int low ,int high){
    if (array == NULL || low>high || low< 0||high<0){
        return ;
    }
    int temp = array[0];
    if(low<high && array[high]> temp){
        high --;
    }
    array[low] = array[high];
    if (low<high && array[low]<temp){
        low++;
    }
    array[high] = array[low];
    array[low] = temp;
    return low;

}


int quick_sort (int array[],int low ,int high){
    if (low == high ){
        return;
    }
    int index  = Partation (array,low,high);
    Partation (array,index+1,high);
    Partation (array,0,index -1);


}


void sortage(int ages[],int length){


    if (ages==NULL || length <0){
        return;
    }
    int oldest = 99;
    int array[oldest+1];
    for (int i =0; i<oldest;i++){
        array[i] = 0;
    }
    for (int j = 0; j<length; j++){
        int temp_age = ages[j];
        if(temp_age<0 ||temp_age>99){
            cout<<"wrong ages"<<endl;
        }
        array[temp_age] ++;

    }

    int index;
    for (int i = 0; i<oldest; i++){
        for (int j = 0; j<numbers; j++){
            ages[index] = i;
            index ++;

        }
    }
}



int binarysearch(int array[],int low ,int high,int target){ //递归来实现

    if (low> high || array==NULL){
        return -1;
    }
    int middle = (low + high) /2;
    if (array[middle] == target){
        return  middle;
    }
    else if (array[middle]> target){
        binarysearch (array, 0, middle-1);
    }
    else{
        binarysearch (array,  middle+1,high);
    }

}

int binarysearch(int array[],int n; int target) {
// 非递归实现
while{low<high}{
int middle  = (low + high)/2;
if (array[middle] ==target){
return middle;
} else if (array[middle]>target){
high = middle;
}
else if (array[middle]<target){
low = middle +1;
}
else {
return -1;
}



}

}


int min()(int numbers[], int length){
    if (numbers ==NULL || length <0){
        return ;
    }

    int index1 = 0;
    int index2 = length -1;
    int midindex = index1;

    while(numbers[index1]>=numbers[index2]){
        if (index2 - index1 ==1){
            return index2;
        }
        midindex = (index1 + index2)/2;

        else if (numbers[midindex]<=numbers[index2]){
            index2 =  midindex ;
        }
        else if (numbers[midindex]>=numbers[index1]){
            index1 = midindex;
        }

    }
    return numbers[midindex];

}


long long fab(int n){
    if (n==0){
        return 0;
    }
    if (n==1){
        return 1;
    }
    else {
        return fab(n-1) + fab(n-1);
    }
}

long long fab(int n){
    int array[2] ={0,1};
    if (n<2){
        return array[n];
    }
    int one =0;
    int two =1;
    int sum =0;
    for (int i =2; i<n i++){
        sum = one + two;
        one = two;
        two = sum;
    }
}


int numberof1(int n){
    int count =0;
    unsigned int flag =1;

    while (flag){
        if(n& flag){
            count ++;
        }
    }
    return flag;

}



二叉搜索树的查找:

int find (int x, searchtree t){
    if (t == null){
        return null;

    } else if(x< t){
        find(x,t->left);
    }
    else{
        find (x,t>right);
    }
    else{
        return t;
    }
}

findmin(searchtree * tree){
if (t==NULL){
return NULL;
}
else if (t->left ==NULL){
return t;

}
else {
return findmin(t->left);
}
}
int findmin(searchterr *tree){
    if (t==NUll){
        return NULL;
    }
    while (t->left != NULL){
        t = t->left;
    }
    return t;
}

int findmax(searchtree * t){
    if(t!=NULL){
        return NULL;
    }
    else if (t->right==NULL){
        return t;
    }
    else {
        return findmax(t->right);
    }
}

searchtree insert(ElementType x, SearachTree Tree){
    if (Tree== NULL){
        Tree =  (searchtree) malloc (sizeof(struct Treenode));

        t->element = x;
        t->left = NULL;
        tree->right =NULL;
    }else if (x<t->element){
        t->left = insert(x,t->left);
    }
    else{
        t->right = insert(x,t->right);
    }

    return t;
}


void preordersearch(Tree * t){
    if(t == NULL){
        return;
    }
    cout<<t->val<<endl;//先序遍历,根左右
    preordersearch(t->left);
    preordersearch(t->right);
}

void inorder(Tree *t){ //中序遍历,左中右
    if(t==NULL){
        return;
    }
    preordersearch(t->left);
    cout<< t->val<<endl;
    preordersearch(t->right);
}

void tailorder(Tree *t){//后序遍历,左右中
    if (t== NULL){
        return ;
    }
    tailorder(t->left);
    tailorder(t->right);
    cout<<t->val<<endl;
}


int main(){
    int a[] = {1,2,3,4-5,10,-1,-1};
    int *b = (int*) malloc(sizeof(a));
    b[0] = a[0]>0 ? a[0]:0;
    int length = sizeof(a)/sizeof(int);
    for (int i= 0;i<length;i++){
        b[i+1] = b [i] + a[i+1] > a[i+1] ? b [i] +a[i+1] : a[i+1];
    }
    int max = b[0];
    for (int j=0;j<length; j++){
        if(b[i]>max){
            max =b[i];
        }
    }
    cout<<b[i]l

}










void merge(int array,int L;int mid; int R){
int p1 = L,p2 = mid+1, i=0;
int temp[L+R-1];

while (p1<mid && p2<R){

temp[i++] = array[p1] <  array[p2] ? array[p1++] : array[p2++];
}
while (p1<mid){
temp[i++] =  array[p1++];
}

while (p2<R){
temp[i++] =  array[p2++];
}

for (int i =0; i< R+L-1;i++){
array[L+i] = temp[i];
}

}

void sort(int array[], int L,int R){

    if (L<R){
        int mid = (L+R)/2;
        sort(array,L,mid);
        sort(array,mid+1,R);
        merge (array,L,mid,R);
    }

}



//quick sort

void quicksort (int array[], int low, int high){
    if(low<high){
        int index = getindex(array,low,high);
        quicksort(array,index+1,high);
        quicksort(array,0,high);
    }
}

int getindex(int array[],int low, int high){
    int temp = array[low];
    while (low<high){
        if(array[high] >temp ){
            high --;
        }
        array[low] = array[high];
        if(array[low]<temp){
            low++;
        }
        array[high] = array[low];

    }
    array[low] =temp;
    return low;
}

//非递归的形式来实现快排

void quichsortnorecusive(int array[],int low,int high[]){
    stack<int> s;

    s.push(low);
    s.push(high);

    while(!s.empty()){
        int h = s.top(); s.pop();
        int l = s.top(); s.pop();
        int index= getindex(array,l,h);
        if(index-1>l){
            s.push(l);//左边
            s.push(index-1);//右边

        }
        else(index+1<h){
            s.push(index +1);
            s.push(h);
        }

    }

}


int binarytreedepth(Tree *tree){
    if(tree ==NULL){
        return 0;
    }
    int left = binarytreedepth(tree->left);
    int right = binarytreedepth(tree->right);
    return left > right ? left ++ :right++;
}



### tcp ip
### https://zhuanlan.zhihu.com/p/86426969

### c++ 面试题目
        https://www.jianshu.com/p/189956c94cef


ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    if(pListHead ==NULL){
        return NULL;
    }
    ListNode* node = pListHead;
    int count =0;
    while(node!= NULL){
        ++count;
        node = node->next;
    }

    if(k==0 || k>count){
        return  NULL;
    }
    ListNode* first = pListHead;
    ListNode* second = pListHead;
    for(int i = 0;i<k-1; i++){
        first = first->next;
    }
    while ( first->next !=NULL){
        first = first->next;
        second = second->next;
    }

    return  second;
}


ListNode* ReverseList(ListNode* pHead) {
    if(pHead ==NULL){
        return NULL;
    }

    ListNode* cur = pHead;
    ListNode* pre = NULL;
    ListNode* temp = NULL;
    while(cur!=NULL){
        temp = cur->next;// 暂存一下下一个
        cur->next = pre;//当前值志向前一个
        pre = cur;//前一个值向后一步
        cur = temp;//把下一个进行后移
    }

    retur pre;
}









发布了95 篇原创文章 · 获赞 8 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/ttomchy/article/details/104973987
old