leetcode 59.螺旋矩阵

记录一下,觉得倒水思想来做 总体看起来还是比较清晰的。

bash 复制代码
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int a[4][2] = {{0,1}, {1,0}, {0,-1},{-1,0}};

        int direction=0;  //方向
        int num=0;

        int S =n*n;
        int x = 0;
        int y = 0;
        
        vector<vector<int>> Matrix(n, vector<int>(n, 0));

        while(num!=S){
            num++;
            Matrix[x][y]=num;

            x+=a[direction][0];
            y+=a[direction][1];
            if(x>=n || y>=n || x<0 || y<0 || Matrix[x][y]!=0){
                x-=a[direction][0];
                y-=a[direction][1];
                direction = (direction+1)%4;
                x+=a[direction][0];
                y+=a[direction][1];
            } 
        }  
        return  Matrix;

    }
};
相关推荐
m0_675988232 分钟前
Leetcode1206:设计跳表
算法·leetcode·跳表·python3
冠位观测者3 分钟前
【Leetcode 每日一题 - 扩展】1512. 好数对的数目
数据结构·算法·leetcode
Joyner20183 分钟前
python-leetcode-路径总和 III
算法·leetcode·职场和发展
南宫生3 分钟前
力扣每日一题【算法学习day.133】
java·学习·算法·leetcode
_Itachi__5 分钟前
LeetCode 热题 100 560. 和为 K 的子数组
数据结构·算法·leetcode
夏末秋也凉5 分钟前
力扣-贪心-55 跳跃游戏
算法·leetcode·游戏
CodeJourney.24 分钟前
EndNote与Word关联:科研写作的高效助力
数据库·人工智能·算法·架构
大模型铲屎官34 分钟前
哈希表入门到精通:从原理到 Python 实现全解析
开发语言·数据结构·python·算法·哈希算法·哈希表
果壳中的robot1 小时前
【ORB-SLAM3】鲁棒核函数的阈值设置
算法·计算机视觉·机器人
DKPT1 小时前
计算机网络之路由协议(自治系统)
开发语言·笔记·学习·计算机网络·算法