剑指offer之数据结构

#ifndef _LD_ARRAY_H_
#define _LD_ARRAY_H_

#include <stack>
#include <queue>

using namespace std;

//剑指offer,面试题3,P38,二维数组中的查找
bool Find(int *matrix, int rows, int columns, int number){
    bool found = false;

    if (matrix!=nullptr && rows>0 && columns>0)
    {
        int row = 0;
        int column = columns - 1;
        while (row < rows && column >= 0){
            if (matrix[row*columns + column] == number){
                found = true;
                break;
            }
            else if (matrix[row*columns + column]>number){
                column--;
            }
            else{
                row++;
            }
        }
    }

    return found;
}

//面试题4,替换空格
//解法一:时间复杂度为O(N2)
void ReplaceBlank1(char* str, int length){
    if (str != nullptr && length>0){
        cout << str << endl;
        int len = 0;
        int i = 0;
        while (str[i] != '\0'){
            len++;
            i++;
        }
        for (int i = 0; i < len; i++){
            if (str[i] == ' '){
                for (int j = len; j>i; j--){
                    str[j + 2] = str[j];
                }
                str[i] = '%';
                str[i + 1] = '2';
                str[i + 2] = '0';
                len += 2;
            }
        }

        for (int i = 0; i < len; i++){
            cout << str[i];
        }
    }
}
//解法二:时间复杂度为O(N)
void ReplaceBlank2(char str[], int length){
    if (str != NULL && length>0){
        int original_length = 0;
        int blank_number = 0;
        int i = 0;

        while (str[i] != '\0'){
            original_length++;
            if (str[i] == ' '){
                blank_number++;
            }
            i++;
        }

        int new_length = original_length + blank_number * 2;
        int index_of_original = original_length;
        int index_of_new = new_length;
        while (index_of_original >= 0 && index_of_new > index_of_original){
            if (str[index_of_original] == ' '){
                str[index_of_new--] = '0';
                str[index_of_new--] = '2';
                str[index_of_new--] = '%';
            }
            else{
                str[index_of_new] = str[index_of_original];
                index_of_new--; 
            }
            index_of_original--;
        }

    }
}

//相关题目
//A1末尾有足够多的空余空间,从小到大排序
void MergeTwoArrays(int A1[], int m, int A2[], int n){
    if (A2 != NULL && n > 0){

        int index1 = m - 1;
        int index2 = n - 1;
        int new_index = m + n - 1;
        while (index1 >= 0 && index2 >= 0){
            if (A1[index1] >= A2[index2]){
                A1[new_index--] = A1[index1--];
            }
            else{
                A1[new_index--] = A2[index2--];
            }
        }
        while (index2 >= 0){
            A1[index2] = A2[index2];
            index2--;
        }
        /*if (index2 > 0){
            for (int i = 0; i <= index2; i++){
            A1[i] = A2[i];
            }
            }*/


        for (int i = 0; i < m+n; i++){
            cout << A1[i] << ends;
        }
    }
}

//面试题6,重建二叉树
struct BinaryTreeNode{
    int m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};

BinaryTreeNode* ConstructCore(int* start_preorder, int* end_preorder,
    int* start_inorder, int* end_inorder);

BinaryTreeNode* Construct(int preorder[], int inorder[], int length){
    if (preorder == NULL || inorder == NULL || length <= 0){
        return NULL;
    }
    else{
        return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
    }
}

BinaryTreeNode* ConstructCore(int* start_preorder, int* end_preorder,
    int* start_inorder, int* end_inorder){

    //构造根节点。
    int root_value = start_preorder[0];
    BinaryTreeNode* root = new BinaryTreeNode();
    root->m_nValue = root_value;
    root->m_pLeft = root->m_pRight = NULL;

    if (start_preorder == end_preorder){
        return root;
    }

    int* root_inorder = start_inorder;
    while (root_inorder <= end_inorder && *root_inorder != root_value){
        root_inorder++;
    }

    int left_length = root_inorder - start_inorder;
    int* left_end_preorder = start_preorder + left_length;
    if (left_length > 0){
        root->m_pLeft=ConstructCore(start_preorder+1, left_end_preorder, start_inorder, root_inorder - 1);
    }
    if (left_length < end_preorder - start_preorder){
        root->m_pRight = ConstructCore(start_preorder + left_length + 1, end_preorder, root_inorder + 1, end_inorder);
    }

    return root;

}
//先序遍历二叉树
void visit1(BinaryTreeNode* T){
    if (T != NULL){
        BinaryTreeNode* P = new BinaryTreeNode();
        P = T;
        cout << P->m_nValue << ends;
        visit1(P->m_pLeft);
        visit1(P->m_pRight);
    }
}
//中序遍历二叉树
void visit2(BinaryTreeNode* T){
    if (T != NULL){
        BinaryTreeNode* P = new BinaryTreeNode();
        P = T;

        visit2(P->m_pLeft);
        cout << P->m_nValue << ends;
        visit2(P->m_pRight);
    }
}


//面试题7,用两个栈实现队列
template<typename T>
class CQueue{
public:
    CQueue() = default;
    ~CQueue()=default;

    void Push(const T& node){
        stack1.push(node);
    }

    T Pop(){
        T t;
        if (!stack2.empty()){
            t = stack2.top();
            stack2.pop();
        }
        else{
            for (int i = stack1.size(); i > 0; i--){
                stack2.push(stack1.top());
                stack1.pop();
            }
            t = stack2.top();
            stack2.pop();
        }
        return t;
    }

private:
    stack<T> stack1;
    stack<T> stack2;
};

//template<typename T>
//void CQueue::Push(const T& node){
//  stack1.push(node);
//}
//
//template<typename T>
//T CQueue::Pop(){
//  T t;
//  if (!stack2.empty()){
//      t = stack2.pop();
//  }
//  else{
//      for (int i = stack1.size(); i > 0; i--){
//          stack2.push(stack1.pop());
//      }
//      t = stack2.pop();
//  }
//  return t;
//}

//相关题目,用两个队列实现一个栈
template<typename T>
class CStack{
public:
    CStack() = default;
    ~CStack() = default;

    void Push(const T& element){
        queue1.push(element);
    }
    T Pop(){
        T t;
        size_t size;
        if (!queue1.empty()){
            size=queue1.size();  
            //queue1.size()必须放在外面,
            //如果写成 for(size_t i=1;i<queue1.size();i++),则queue1.size()在每次循环都重新求值,所得就不是原来的size了。
            for (size_t i = 1; i < size; i++){
                t = queue1.front();
                queue1.pop();
                queue2.push(t);
            }
            t = queue1.front();
            queue1.pop();
        }
        else if (!queue2.empty()){
            size = queue2.size();
            for (int i = 1; i < size; i++){
                t = queue2.front();
                queue2.pop();
                queue1.push(t);
            }
            t = queue2.front();
            queue2.pop();
        }
        else{
            t = NULL;
        }

        return t;
    }

public:
    queue<T> queue1;
    queue<T> queue2;
};


#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值