[LeetCode 115 C++ Questions] Spiral Matrix螺旋矩阵

54. Spiral Matrix螺旋矩阵

Given an m x n matrix, return all elements of the matrix in spiral order.

给定一个 m x n 矩阵,按螺旋顺序返回矩阵的所有元素。

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int index_i = 0, index_j = -1, edge_i = matrix.size(), edge_j = matrix[0].size();
        vector<int> ans;
        int totalCnt = edge_i * edge_j;
        while(totalCnt){
            //向右移动
            while(totalCnt > 0 && index_j + 1 < edge_j && matrix[index_i][index_j + 1] != -200){
                index_j++;
                ans.push_back(matrix[index_i][index_j]);
                matrix[index_i][index_j] = -200;
                totalCnt--;
            }
            //向下移动
            while(totalCnt > 0 && index_i + 1 < edge_i && matrix[index_i + 1][index_j] != -200){
                index_i++;
                ans.push_back(matrix[index_i][index_j]);
                matrix[index_i][index_j] = -200;
                totalCnt--;
            }
            //向左移动
            while(totalCnt > 0 && index_j - 1 >= 0 && matrix[index_i][index_j - 1] != -200){
                index_j--;
                ans.push_back(matrix[index_i][index_j]);
                matrix[index_i][index_j] = -200;
                totalCnt--;
            }
            //向上移动
            while(totalCnt > 0 && index_i - 1 >= 0 && matrix[index_i - 1][index_j] != -200){
                index_i--;
                ans.push_back(matrix[index_i][index_j]);
                matrix[index_i][index_j] = -200;
                totalCnt--;
            }
        }
        return ans;
    }
};

59. Spiral Matrix II螺旋矩阵

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

给定一个正整数 n,生成一个 n x n 矩阵,按螺旋顺序填充从 1 到 n2 的元素。

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int count=1;
        vector<vector<int>> res(n, vector<int>(n, 0));
        int start_x, start_y, offset, j, i, loop;
        start_x=start_y=0;
        offset=1;
        loop=n/2;
        while(loop--){
            for(j=start_y;j<n-offset;j++)
                res[start_x][j]=count++;
            for(i=start_x;i<n-offset;i++)
                res[i][j]=count++;
            for(;j>start_y;--j)
                res[i][j]=count++;
            for(;i>start_x;--i)
                res[i][j]=count++;
            start_y++;
            start_x++;
            offset++;
        }
        if(n%2==1)
            res[n/2][n/2]=count++;
        return res;
    }
};

885. Spiral Matrix III螺旋矩阵

You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.

Return an array of coordinates representing the positions of the grid in the order you visited them.

从朝东的行 x 列网格的单元格 (rStart, cStart) 开始。西北角位于网格中的第一行和列,东南角位于最后一行和最后一列。
您将以顺时针螺旋形状行走,以访问此网格中的每个位置。每当您移出网格的边界时,我们都会继续在网格外行走(但稍后可能会返回到网格边界)。最终,我们到达网格的所有行 * 列空格。
返回一个坐标数组,表示网格的位置,按您访问它们的顺序排列。
 

class Solution {
public:
    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
        vector<vector<int>> res;
        int dx = 0, dy = 1;
        int step = 1;
        int count = 1;
        res.push_back(vector<int>{rStart, cStart});
        while (count < rows * cols) {
            dx = 0, dy = 1;
            for (int s = 0; s < step; s++) {
                rStart += dx;
                cStart += dy;
                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
                    res.push_back(vector<int>{rStart, cStart});
                    count ++;
                    if (count >= rows * cols) return res;
                }
            }
            dx = 1, dy = 0;
            for (int s = 0; s < step; s++) {
                rStart += dx;
                cStart += dy;
                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
                    res.push_back(vector<int>{rStart, cStart});
                    count ++;
                    if (count >= rows * cols) return res;
                }
            }
            step ++;
            dx = 0, dy = -1;
            for (int s = 0; s < step; s++) {
                rStart += dx;
                cStart += dy;
                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
                    res.push_back(vector<int>{rStart, cStart});
                    count ++;
                    if (count >= rows * cols) return res;
                }
            }
            dx = -1, dy = 0;
            for (int s = 0; s < step; s++) {
                rStart += dx;
                cStart += dy;
                if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
                    res.push_back(vector<int>{rStart, cStart});
                    count ++;
                    if (count >= rows * cols) return res;
                }
            }
            step ++;
        }
        return res;
    }
};

2326. Spiral Matrix IV螺旋矩阵

You are given two integers m and n, which represent the dimensions of a matrix.

You are also given the head of a linked list of integers.

Generate an m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix. If there are remaining empty spaces, fill them with -1.

Return the generated matrix.

你得到两个整数 m 和 n,它们代表矩阵的维度。

您还将获得整数链表的头部。

生成一个 m x n 矩阵,该矩阵包含链表中的整数,从矩阵的左上角开始,以螺旋顺序(顺时针)显示。如果还有剩余的空格,请用 -1 填充它们。

返回生成的矩阵。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> spiralMatrix(int n, int m, ListNode* head) 
    {
        // Create a matrix of n x m with values filed with -1.
        vector<vector<int>> spiral(n, vector<int>(m, -1));
        int u=0, d=n-1, l=0, r=m-1;

        while(head!=NULL){
            //up
            for(int col=l; col<=r && head!=NULL && spiral[u][col]==-1;col++){
                spiral[u][col]=head->val;
                head=head->next;
            }
            if(head==NULL || ++u > d)
                break;
            
            //right
            for(int row=u; row<=d && head!=NULL && spiral[row][r]==-1;row++){
                spiral[row][r]=head->val;
                head=head->next;
            }
            if(head==NULL || --r<l)
                break;

            //down
            for(int col=r; col>=l && head!=NULL && spiral[d][col]==-1;col--){
                spiral[d][col]=head->val;
                head=head->next;
            }
            if(head==NULL || --d<u)
                break;
            
            //left
            for(int row=d; row>=u && head!=NULL && spiral[row][l] == -1; row--){
                spiral[row][l] = head->val;
                head = head->next;
            }
            if(head==NULL || ++l>r)
                break;
        }
        //Rest values are itself -1.
        return spiral;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值