算法初级02

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zxt_1/article/details/88430470

1.用数组结构实现大小固定的栈和队列

class arrayStack
{
private:
    int *arr;
    int len;
    int index;  //指向待插入位置

public:
    arrayStack(int initSize)
    {
        if(initSize < 0)    
            throw "initSize is too small";
    
        arr = new int[initSize];
        len = initSize;
        index = 0;
    }
    
    void push(int obj)
    {
        if(index == len)
            throw "the stack is full";
        
        arr[index++] = obj;
    }

    void pop()
    {
        if(index == 0)
            throw "the stack is empty";
        
        return arr[--index];
    }
};
class arrayQueue
{
private:
    int *arr;
    int size;
    int capacity;
    int start;  //指向待出队位置
    int end;    //指向待插入位置

public:
    arrayQueue(int initSize)
    {
        if(initSize <= 0)
            throw "initSize is too small";
        
        arr = new int[initSize];
        capacity = initSize;
        size = 0;
        start = 0;
        end = 0;
    }

    void push(int obj)
    {
        if(size == capacity)
            throw "the queue is full";
        
        arr[end] = obj;
        size++;
        end = (end==capacity-1)?0:end+1;
    }

    int pop()
    {
        if(size == 0)
            throw "the queue is empty";
        
        int ret = arr[start];
        start = (start==capacity-1)?0:start+1;
        return ret;
    }
};

2.实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作

class MyStack
{
private:
    stack<int> stackData;
    stack<int> stackMin;

public:
    MyStack()
    {
        
    }

    void push(int obj)
    {
        if(stackMin.empty())
            stackMin.push(obj);
        else if(stackMin.top() < obj)
            stackMin.push(obj);
        else{
            int Min = stackMin.top();
            stackMin.push(Min);
        }

        stackData.push(obj);
    }

    int pop()
    {
        if(stackData.empty())
            throw "stack is empty";
        stackMin.pop();
        int ret = stackData.top();
        stackData.pop();
        return ret;
    }

    int getMin()
    {
        if(stackMin.empty())
            throw "stack is empty";

        return stackMin.top();
    }
};

3.仅用队列结构实现栈结构、仅用栈结构实现队列结构

class TwoQueuesStack
{
private:
    queue<int> data;
    queue<int> help;

    void _swap()
    {
        queue<int> tmp;
        tmp = data;
        data = help;
        help = tmp;
    }

public:
    TwoQueuesStack(){}

    void push(int obj)
    {
        data.push(obj);
    }

    int pop()
    {
        if(data.empty())
            throw "stack is empty";
        
        while(data.size() > 1)
        {
            help.push(data.front());
            data.pop();
        }

        int ret = data.front();
        data.pop();
        _swap(); 

        return ret;
    }

};
class TwoStacksQueue
{
private:
    stack<int> stackPush;
    stack<int> stackPop;

public:
    TwoStacksQueue(){}

    void push(int obj)
    {
        stackPush.push(obj);
    }

    int pop()
    {
        if(stackPush.empty() && stackPop.empty())
            throw "queue is empty";
        else if(stackPop.empty()){
            while(!stackPush.empty())
            {
                stackPop.push(stackPush.top());
                stackPush.pop();
            }
        }

        int ret = stackPop.top();
        stackPop.pop();
        return ret;
    }
};

4.“之”字形打印矩阵

在这里插入图片描述

void printMatrixZigZag(int **matrix, int row, int col)
{
    int aR = 0;
    int aC = 0;
    int bR = 0;
    int bC = 0;

    int endR = row-1;
    int endC = col-1;

    bool dir = false;   //斜上
    while(aR != endR+1)
    {
        printLevel(matrix, aR, aC, bR, bC, dir);
        aR = (aC==endC)?aR+1:aR;
		aC = (aC==endC)?aC:aC+1;
		bC = (bR==endR)?bC+1:bC;
		bR = (bR==endR)?bR:bR+1;

        dir = !dir;
    }

}

void printLevel(int **matrix, int aR, int aC, int bR, int bC, bool dir)
{
    if(dir)
    {
        while(aR != bR+1)
            cout<<matrix[aR++][aC--]<<" ";
    }
    else{
        while(bR != aR-1)
            cout<<matrix[bR--][bC++]<<" ";
    }
}

5. 在行列都排好序的矩阵中找数

在这里插入图片描述

bool findNum(int **matrix, int row, int col, int target)
{
    int X = 0;
    int Y = col-1;

    while(X<col && Y>=0)
    {
        if(matrix[X][Y] == target)
            return true;
        if(matrix[X][Y] < target)
            X++;
        else
            Y--;
    }
   
    return false;
}

6.将单向链表按某值划分成左边小、中间相等、右边大的形式

在这里插入图片描述

Node *listPartition(Node *head, int pivot)
{
    Node *sHead;
    Node *sTail;

    Node *eHead;
    Node *eTail;

    Node *bHead;
    Node *bTail;

    Node *tmp;
    while(head)
    {
        tmp = head->next;
        head->next = NULL;
        if(head->val < pivot)
        {
            if(!sHead)
            {
                sHead = head;
                sTail = head;
            }
            else
            {
                sTail->next = head;
                sTail = head;
            }
        }else if(head->val == pivot)
        {
            if(!eHead)
            {
                eHead = head;
                eTail = head;
            }
            else
            {
                eTail->next = head;
                eTail = head;
            }
        }else{
            if(!bHead)
            {
                bHead = head;
                bTail = head;
            }
            else
            {
                bTail->next = head;
                bTail = head;
            }
        }

        head = tmp;
    }

    if(sTail){
        sTail->next = eHead;
        eTail = (eTail==NULL)?sTail:eTail;
    }

    if(eTail)
        eTail->next = bHead;

    return sHead?sHead:eHead?eHead:bHead;
}

7. 复制含有随机指针节点的链表

在这里插入图片描述

Node *copyList(Node *head)
{
    if(!head)   return NULL;

    Node *cur = head;
    Node *tmp = NULL;

    while(cur)
    {
        tmp = cur->next;
        cur->next = new Node(cur->val);
        cur->next->next = tmp;

        cur = tmp;
    }

    cur = head;
    Node *curCopy = NULL;
    while(cur)
    {
        tmp = cur->next->next;
        curCopy = cur->next;
        curCopy->rand = (cur->rand)?cur->rand->next:NULL;

        cur = tmp;
    }

    Node *ret = head->next;
    cur = head;
    while(cur)
    {
        tmp = cur->next->next;
        curCopy = cur->next;
        cur->next = tmp;
        curCopy->next = (tmp)?tmp->next:NULL;

        cur = tmp;
    }

    return ret;
}

8. 判断两个单链表相交,若相交返回第一个相交节点

在这里插入图片描述
在这里插入图片描述

Node *getIntersectNode(Node *head1, Node *head2)
{
    if(!head1 || !head2)    return NULL;

    Node *loop1 = getLoopNode(head1);
    Node *loop2 = getLoopNode(head2);
    if(!loop1 && !loop2)        //两链表无环
    {
        return noLoop(head1, head2);
    }

    if(loop1 && loop2)          //两链表有环
    {
        return bothLoop(head1, loop1, head2, loop2);
    }

    return NULL;        //一个链表有环,一个无环
}


Node *getLoopNode(Node *head)
{
    if(!head || !head->next || !head->next->next)   return NULL;

    Node *slow = head->next;
    Node *fast = head->next->next;
    while(slow != fast)
    {
        if(fast->next==NULL || fast->next->next==NULL)
            return NULL;
        
        fast = fast->next->next;
        slow = slow->next;
    }

    fast = head;
    while(slow != fast)
    {
        slow = slow->next;
        fast = fast->next;
    }

    return fast;
}

Node *noLoop(Node *head1, Node *head2)
{
    if(!head1 || !head2)    return NULL;

    Node *p1 = head1;
    Node *p2 = head2;
    int n = 0;
    while(p1->next)
    {
        n++;
        p1= p1->next;
    }

    while(p2->next)
    {
        n--;
        p2 = p2->next;
    }

    if(p1 != p2)    return NULL;    //末尾指针不等,肯定不相交

    p1 = (n>0)?head1:head2;     //p1指向长链表
    p2 = (p1==head1)?head2:head1;
    n = abs(n);
    while(n > 0)
    {
        n--;
        p1 = p1->next;
    }

    while(p1 != p2)
    {
        p1 = p1->next;
        p2 = p2->next;
    }

    return p1;
}

Node *bothLoop(Node *head1, Node *loop1, Node *head2, Node *loop2)
{
    Node *p1 = NULL;
    Node *p2 = NULL;

    if(loop1 == loop2)
    {
        p1 = head1;
        p2 = head2;
        int n = 0;

        while(p1 != loop1)
        {
            n++;
            p1= p1->next;
        }

        while(p2 != loop2)
        {
            n--;
            p2 = p2->next;
        }

        p1 = (n>0)?head1:head2;
        p2 = (p1==head1)?head2:head1;
        n = abs(n);
        while(n > 0)
        {
            n--;
            p1 = p1->next;
        }

        while(p1 != p2)
        {
            p1 = p1->next;
            p2 = p2->next;
        }

        return p1;
    }else{
        p1 = loop1->next;
        while(p1 != loop1)
        {
            if(p1 == loop2)
                return loop1;   //return loop2;
            p1 = p1->next;
        }

        return NULL;
    }
}

猜你喜欢

转载自blog.csdn.net/zxt_1/article/details/88430470
今日推荐