剑指offer(19~21)--顺时针打印矩阵--包含min函数的栈--栈的压入弹出序列

版权声明:允许转载,请注明文章出处 https://blog.csdn.net/Vickers_xiaowei/article/details/86565149

19、顺时针打印矩阵

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
在这里插入图片描述
难点:一圈一圈打印,只要是下标控制,最后的判断也是必要的。

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        int row = matrix.size();
        int col = matrix[0].size();
        vector<int> res;
         
        // 输入的二维数组非法,返回空的数组
        if (row == 0 || col == 0)  return res;
         
        // 定义四个关键变量,表示左上和右下的打印范围
        int left = 0, top = 0, right = col - 1, bottom = row - 1;
        while (left <= right && top <= bottom)
        {
            // left to right
            for (int i = left; i <= right; ++i)  res.push_back(matrix[top][i]);
            // top to bottom
            for (int i = top + 1; i <= bottom; ++i)  res.push_back(matrix[i][right]);
            // right to left
            if (top != bottom)
            for (int i = right - 1; i >= left; --i)  res.push_back(matrix[bottom][i]);
            // bottom to top
            if (left != right)
            for (int i = bottom - 1; i > top; --i)  res.push_back(matrix[i][left]);
            left++,top++,right--,bottom--;
        }
        return res;
    }
};

牛客网链接

20、包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
在这里插入图片描述
用一个m栈来存储最小数,每次插入数据都与m栈的栈顶数据比较,实时更新m栈顶,使m栈顶一直是那个时候整个栈中最小的数据。pop栈s栈顶数据时候,如果s栈顶数据和栈顶数据相等,那么说明整个最小数据要pop了,就也要pop掉m栈的栈顶数据。

class Solution {
public:
    void push(int value) {
        s.push(value);
        if(m.empty())
            m.push(value);
        else
        {
            if(value<=m.top())
                m.push(value);
        }
    }
    void pop() {
        if(s.top()==m.top())
            m.pop();
        s.pop();
    }
    int top() {
        return s.top();
    }
    int min() {
        return m.top();
    }
private:
    stack<int> s;
    stack<int> m;
};

牛客网链接

21、栈的压入弹出序列

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

模拟入栈出栈过程
出栈序列4出栈说明,对比入栈序列,说明入栈的1,2,3还在栈里。那我们对比发现这三个数不等于4,让它出栈,等于4,说明入栈又立马出栈了。出栈序列和入栈序列都++,比较下一个元素。当入栈序列走到尾的时候,这些元素要么入栈后已经出栈,要么还在栈了。那么我们将栈中数据与出栈序列一一对比,按序相等就合法,否则不合法。

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        int in=0,out=0,end1=pushV.size(),end2=popV.size();
        stack<int> s;
        
        while(in<end1)
        {
            if(pushV[in]==popV[out])//数据入栈立马出栈
            {
                out++,in++;
            }
            else
            {
                s.push(pushV[in]);
                in++;
            }
            
            while(!s.empty()&&s.top()==popV[out])//元素出栈
            {
                s.pop();
                out++;
            }
        }
        while(out<end2)
        {
            if(popV[out]!=s.top())return false;
            else
            {
                s.pop();
                out++;
            }
        }
        return true;
    }
};

牛客网链接

猜你喜欢

转载自blog.csdn.net/Vickers_xiaowei/article/details/86565149