c++数据结构与算法(6)——数组(螺旋矩阵)

原来很怕的一种题型,但是面试的时候又经常碰到,今天算是硬啃下来了

典型题目

54. 螺旋矩阵

59. 螺旋矩阵 II

885. 螺旋矩阵 III

————————————

54. 螺旋矩阵

按照遍历方式层层剥开,该类型题方法比较固定,建议理解记忆

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> ret;
        int m = matrix.size();
        if (m == 0) return ret;
        int n = matrix[0].size();
        int size = m*n, i = 0, left = 0, top = 0, right = n-1, bottom = m-1;
        int row, col;
        ret.resize(size);
        while (i < size) {
            for (row = top, col = left; col <= right && i < size; col++, i++) {
                ret[i] = matrix[row][col];
            }
            top++;
            for (row = top, col = right; row <= bottom && i < size; row++, i++) {
                ret[i] = matrix[row][col];
            }
            right--;
            for (row = bottom, col = right; col >= left && i < size ;col--, i++) {
                ret[i] = matrix[row][col];
            }
            bottom--;
            for (row = bottom, col = left; row >= top && i < size;row--, i++) {
                ret[i] = matrix[row][col];
            }
            left++;
        }
        return ret;
    }
};

59. 螺旋矩阵 II

跟上一题思想没有任何区别,就是输入、输出调过来,这并不是难点

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> ret(n, vector<int>(n));
        int size = n*n, i = 1, left = 0, top = 0, right = n-1, bottom = n-1,row,col;
        while (i <= size) {
            for (row = top, col = left; col <= right && i <= size; col++, i++) {
                ret[row][col] = i;
            }
            top++;
            for (row = top, col = right; row <= bottom && i <= size; row++, i++) {
                ret[row][col] = i;
            }
            right--;
            for (row = bottom, col = right; col >= left && i <= size ;col--, i++) {
                ret[row][col] = i;
            }
            bottom--;
            for (row = bottom, col = left; row >= top && i <= size;row--, i++) {
                ret[row][col] = i;
            }
            left++;
        }
        return ret;
    }
};

885. 螺旋矩阵 III

这个题难很多,手动遍历思考就会发现前两题是层层剥开,这个题是层层加衣,思路出来,剩余的就是细节了,我偷懒空白部分也进行遍历,就是输出前做下判断

class Solution {
public:
    bool issafe(int row, int col, int rows, int cols)
    {
        if (row >= 0 && row < rows && col >= 0 && col < cols) return true;
        return false;
    }
    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
        int size = rows*cols, i = 1, left = cStart, top = rStart, right = left, bottom = top;
        vector<vector<int>> ret(size, vector<int>(2));
        int row,col;
        ret[0][0] = rStart;
        ret[0][1] = cStart;
        while (i < size) {
            right++;
            for (row = top, col = left+1; col <= right && i < size; col++) {
                if (issafe(row, col, rows, cols)) {
                    //printf("[%d][%d][%d]1,",row,col,i);
                    ret[i][0] = row;
                    ret[i][1] = col;
                    i++;
                }
            }
            bottom++;
            for (row = top+1, col = right; row <= bottom && i < size; row++) {
                if (issafe(row, col, rows, cols)) {
                    //printf("[%d][%d][%d]1,",row,col,i);
                    ret[i][0] = row;
                    ret[i][1] = col;
                    i++;
                }
            }
            left--;
            for (row = bottom, col = right-1; col >= left && i < size; col--) {
                if (issafe(row, col, rows, cols)) {
                    //printf("[%d][%d][%d]1,",row,col,i);
                    ret[i][0] = row;
                    ret[i][1] = col;
                    i++;
                }
            }
            top--;
            for (row = bottom-1, col = left; row >= top && i < size; row--) {
                if (issafe(row, col, rows, cols)) {
                    //printf("[%d][%d][%d]1,",row,col,i);
                    ret[i][0] = row;
                    ret[i][1] = col;
                    i++;
                }          
            }
        }
        return ret;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值